Create a string list variable

I would like to know if the contents of the variable list can be used to create a new variable.

As an example:

str={"cow","monkey"} 

these lines are extracted from the file. Now I would like to refer to these lines as a variable. Thus, the cow variable can be set to {4,2,3} or something else. Any link like str[[1]] , of course, gives the string "cow."

Any clues or is this a bad idea?

Of course, I could add the information to the list that I already have:

 str={{"cow",{4,2,3}},{"monkey",{}} 

but I still can’t directly access Korea as a variable.

+4
source share
3 answers

The simplest would be to simply manually use the cow and monkey characters, not the strings:

 In[309]:= cow = 1; monkey = 2; {cow, monkey} Out[311]= {1, 2} 

But this is probably not what you requested. If you want to automatically convert strings to variables, then what should you do (if I understood the question correctly), you must first convert your strings to characters, since characters can be assigned values ​​and used as variables:

 Remove[cow,monkey]; str = {"cow", "monkey"}; str1 = ToExpression /@ str {cow, monkey} 

(I assume that the characters cow and monkey not been used / defined). After that, you can use the answer for this question to assign variables based on their positions in str1 . However, the usefulness of this approach is also in doubt.

I think the most important thing is to create the so-called indexed variables , for example

 myIndexedVar["cow"] = 1; myIndexedVar["monkey"] = 2; 

where myIndexedVar is essentially a hash table of key-value pairs, and the keys are your rows and values ​​that you want to assign to them. If necessary, the process can be automated.

EDIT

To illustrate assignments to such variables, here is a function that automates the following:

 assignVariables[varNames_List, values_List, hashName_Symbol ] /; Length[varNames] == Length[values] := MapThread[(hashName[#1] = #2) &, {varNames, values}]; 

Here is how you can use it:

 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]= {} 

But then again, this is really a hash table, so your question makes sense to me when it is reformulated like this: "I want to create a hash table with string keys. What is the easiest way to do this in Mathematica, add key-value pairs and access to them". The answer seems to be indexed variables, as shown above. You might also find it useful to read DownValues , as they provide a mechanism for indexed variables.

+4
source

ToExpression converts a string to an expression. Symbol["x"] creates the symbol x, as the name suggests.

However, I wonder how you plan to use them. If you do not know a priori what is in the file with names, how are you going to use them? If str={"cow","monkey"} and I create a list of characters with strSym=ToExpression/@str , then the only way I could continue in my code is to index this second array. I can’t just say cow=5 in my code, because at the time of writing, I don’t know that there will be a variable called cow.

 In[1]:= str = {"cow", "monkey"}; strSym = ToExpression /@ str Out[2]= {cow, monkey} In[3]:= strSym[[1]] // Evaluate = 5 Out[3]= 5 In[4]:= ?cow Global`cow cow=5 

As you can see, indexing also requires an additional Evaluate, because Set (=) has HoldFirst as an attribute. Therefore, I cannot set a cow by indexing, unless the index has been evaluated otherwise, I rewrite the definition of strSym [[1]].

+3
source

Leonid's last method is probably the best, but I like the replacement rules, so I suggest:

 str={"cow","monkey"}; rules = {"cow" -> {4,2,3}, "monkey" -> {}}; str[[1]] /. rules 
  Out = {4, 2, 3} 

See Rule and ReplaceAll for more.

+3
source

All Articles