Message Boards Message Boards

0
|
5513 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Get all the symbols with a pattern and save their Own, Down & Up values?

Example question: I look for all the symbols with a pattern: foundVars=Names["var*"]. I want to get all their Own, Down, and Up values and save them. I can do this with Save but that's not quite my question. I can't do this: DownValues/@foundVars

Example Question 2: I create a function to check symbol naming:

Attributes[checkName]=HoldAll
checkName[x_]:=StringMatchQ[SymbolName[x],"var*"]

But I run into this:

in:     var3=.;    (*Clear var3*)
in:     checkName[var3] 
out:    True   (*so far so good*)
in:     var3 = 2;  (*give var3 a value*)
in:     checkName[var3]
out:         SymbolName: Argument 2 at position 1 is expected to be a symbol

Looking at the results from DownValues, OwnValues, etc. I'm guessing the trick has something to do with HoldPattern, but I haven't been able to piece it together. Let me know some tricks and best practices when working with symbols.

POSTED BY: Eric Smith
5 Replies
Anonymous User
Anonymous User
Posted 6 years ago

if your trying to preserve them (you said "save them" generically)...

you can save the (whole session), a function, or expressions and restore them as .mx i believe. it saves all pertinent info including up/down values.

(you don't need to extract info to "save them")

POSTED BY: Anonymous User

Try something like

In[1]:= var3 = 2;

In[2]:= ToExpression[#, InputForm, OwnValues] & /@ Names["var*"]

Out[2]= {{HoldPattern[var3] :> 2}}

In[3]:= Attributes[checkName] = HoldAll;
        checkName[x_] := StringMatchQ[SymbolName[Unevaluated[x]], "var*"]

In[5]:= checkName[var3]

Out[5]= True
POSTED BY: Ilian Gachevski

Thank you Illian, that gives me some hints. I've never used the third argument of ToExpression and haven't been able to use Unevaluated effectively. Great suggestions!

Appreciate the help!

POSTED BY: Eric Smith

This is a good tutorial on working with unevaluated expressions:

POSTED BY: Szabolcs Horvát

Awesome. I meant to ask for any kind of tutorial, class, or presentation on this topic. It's been a mystery to me for years. Thanks for the link.

POSTED BY: Eric Smith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract