6
$\begingroup$

I have the list

n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv\"}}. 

That is {{variable name, string}, {name, string}}. I want the variable REL1 to be set to "REL.csv". How can I do that?

n1[1,1]] = n1[1,2]] does not work.

$\endgroup$
  • 4
    $\begingroup$ If you want each name Set to the associated string use Set @@@ n1 $\endgroup$ – Bob Hanlon Apr 9 at 21:25
5
$\begingroup$

Still another approach is

Clear[REL1, RAND1, n1]; 
n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};
Set @@ First[n1];

which yields

{REL1, RAND1}
(* {"REL.csv", RAND1} *)

as expected. To perform this process on the whole list, use

Clear[REL1, RAND1, n1]; 
n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};
Set @@@ n1;

which yields

{REL1, RAND1}
(* {"REL.csv", "RAND.csv"} *)
| improve this answer | |
$\endgroup$
3
$\begingroup$

You can do this using replacements by replacing List with Set. For example:

n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};
Evaluate[n1[[1]] /. List -> Set]

Now REL1 is REL.csv

You can do a whole list of such things:

Clear[REL1, RAND1];
n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};
Table[Evaluate[n1[[i]]] /. List -> Set, {i, Length[n1]}]
| improve this answer | |
$\endgroup$
1
$\begingroup$
ClearAll[REL1, RAND1];
n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};
(Evaluate@First@# = Last@#) & /@ n1;
{REL1, RAND1}
(* {"REL.csv", "RAND.csv"} *)
| improve this answer | |
$\endgroup$
1
$\begingroup$

Applying set at level 1

n1 = {{REL1, "REL.csv"}, {RAND1, "RAND.csv"}};

(#1 = #2) & @@@ n1;

{REL1, RAND1}

{"REL.csv", "RAND.csv"}
| improve this answer | |
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.