Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest would be to just manually use symbols <code>cow</code> and <code>monkey</code> rather than strings:</p> <pre><code>In[309]:= cow = 1; monkey = 2; {cow, monkey} Out[311]= {1, 2} </code></pre> <p>But this is probably not what you asked. If you want to automatically convert strings to variables, then what you have to do (if I understood the question correctly) is to first convert your strings to symbols, since symbols can be assigned values and used as variables:</p> <pre><code>Remove[cow,monkey]; str = {"cow", "monkey"}; str1 = ToExpression /@ str {cow, monkey} </code></pre> <p>(I assume that symbols <code>cow</code> and <code>monkey</code> have not been used/defined). After that, you can use the answer for <a href="https://stackoverflow.com/questions/5708142/how-to-set-values-to-symbols/5708395">this</a> question to assign to the variables based on their positions in <code>str1</code>. However, the usefulness of this approach is also questionable.</p> <p>What I think makes the most sense is to create so called <a href="http://reference.wolfram.com/mathematica/tutorial/MakingDefinitionsForIndexedObjects.html" rel="nofollow noreferrer">indexed variables</a>, such as </p> <pre><code>myIndexedVar["cow"] = 1; myIndexedVar["monkey"] = 2; </code></pre> <p>where <code>myIndexedVar</code> is essentially a hash-table of key-value pairs, with keys being your strings and values being whatever you want to assign to them. The process can be automated if needed.</p> <p><strong>EDIT</strong></p> <p>To illustrate assignments to such variables, here is a function which automates that:</p> <pre><code>assignVariables[varNames_List, values_List, hashName_Symbol ] /; Length[varNames] == Length[values] := MapThread[(hashName[#1] = #2) &amp;, {varNames, values}]; </code></pre> <p>Here is how you can use it:</p> <pre><code>In[316]:= assignVariables[str,{{4,2,3},{}},myIndexedVar] Out[316]= {{4,2,3},{}} In[317]:= myIndexedVar["cow"] Out[317]= {4,2,3} In[318]:= myIndexedVar["monkey"] Out[318]= {} </code></pre> <p>But again, this really is a hash-table, so your question makes most sense to me when reformulated as: "I want to make a hash-table with string keys. What's the easiest way to do that in Mathematica, add key-value pairs and access them". The answer seems to be - indexed variables, as illustrated above. You may also find it useful to read on <a href="http://reference.wolfram.com/mathematica/ref/DownValues.html" rel="nofollow noreferrer">DownValues</a>, since these provide the mechanism for indexed variables.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload