0
$\begingroup$

I'm trying to DumpSave[] some variables based on their name string. To define them, I'm using

assignValueFromVariableName = Function[{string, value}, Clear[string]; Evaluate@Symbol[string] = value];

which allows me to define and assign a variable from a string. For example,

assignValueFromVariableName["foo", 1]

yields

In[]:= foo

Out[]= 1

However, once it's assigned, I can no longer DumpSave it from it's name. To be clear,

DumpSave["foo.mx", foo]

works properly but

DumpSave["foo.mx", Symbol["foo"]]

does not, since the symbol is evaluated first.

I've tried using Hold[], HoldForm[] and Unevaluated[] but to no avail so far. Is it possibly to save the symbol only by using it's name string?

Also, I've found a workaround with Put[], but it was significantly slower. I'd like to preserve DumpSave[] fast behaviour. Is this possible? If so, how?

Edit

As Carl Woll mentioned,

DumpSave["foo.mx","foo"]

does work in this simplified example. However, in my original code, I'm using this function to generate the names:

generateSolutionName[bOverbMax_, velocity_, \[Alpha]_] := Module[{},
"solvInf"
<> ToString[velocity*10^-3]
<> "b0p"
<> StringExtract[
ToString[NumberForm[FractionalPart[bOverbMax], 32]], "." -> 2]
<> "alpha"
<> ToString[\[Alpha]]
]

which produces something like solvInf5b0p5alpha0. I've confirmed that the output of this function is indeed a string (just like "foo"), but if I try

DumpSave["foo.mx", generateSolutionName[0.5, 5*10^3, 0]]

I get the following error:

DumpSave::bsnosym: generateSolutionName(0.5,5 10^3,0) is not defined as a symbol or a context.

This is why I was trying to use Symbol[] in my original simplified example.

Edit II

Apparently, if I write

DumpSave["foo.mx", Evaluate@generateSolutionName[0.5, 5*10^3, 0]]

it works just fine. However, I came to this merely by experience (like when you have to use Evaluate before a Table inside Plot. If someone could explain why this works, I'd appreciate it. Sorry if it's something basic that I've overlook.

$\endgroup$
  • 1
    $\begingroup$ Did you try using DumpSave["foo.mx", "foo"]? $\endgroup$ – Carl Woll Sep 1 at 17:05
  • $\begingroup$ Well, I did with my original code. Strangely enough, your proposal works in this simplified example, but not on the original. I'll edit the question to try to provide more information. $\endgroup$ – Jak Sep 1 at 17:21
  • 1
    $\begingroup$ DumpSave has the HoldRest attribute. This is why you need the Evaluate. $\endgroup$ – Carl Woll Sep 1 at 18:09
  • $\begingroup$ @CarlWoll Thank you! Not sure how to close this though... If you'd turn your comment into an answer, I'd be happy to accept it. $\endgroup$ – Jak Sep 1 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.