Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert strings inside an expression to symbols without evaluating them further
ResourceFunction["ConvertStringsToSymbols"][expr, str] replaces all instances of str in expr with the symbol of that name. | |
ResourceFunction["ConvertStringsToSymbols"][expr, str→"context`"] creates the symbol in a given context. | |
ResourceFunction["ConvertStringsToSymbols"][expr, {spec1,spec2,…}] replaces multiple strings with symbols. |
Convert "x" to x in a polynomial:
In[1]:= |
Out[2]= |
In[3]:= |
Out[3]= |
If the input expression is in held form, x will not evaluate to any value it might have:
In[4]:= |
Out[5]= |
The replacement is done before the expression is evaluated, so functions like Set (=) and SetDelayed (:=) work as expected:
In[6]:= |
Out[7]= |
The value of x changed:
In[8]:= |
Out[8]= |
Convert multiple strings to symbols:
In[9]:= |
Out[9]= |
In[10]:= |
Out[10]= |
Put x in a different context:
In[11]:= |
Out[11]= |
In[12]:= |
Out[12]= |
Create symbols in different contexts. The context specification Automatic is equivalent to not specifying a context:
In[13]:= |
Out[13]= |
Construct a range of symbols programmatically and populate them with values:
In[14]:= |
Out[14]= |
Check the values:
In[15]:= |
Out[15]= |
This works even when the symbols already have values:
In[16]:= |
Out[17]= |
This work is licensed under a Creative Commons Attribution 4.0 International License