1
$\begingroup$

I have a list like this:

list = {"k1","k2"} 

I want to use the elements in the list as Table indicators:

Table[k1 + k2, {ToExpression@list[[1]], 1, 2}, {ToExpression@list[[2]], 1, 2}] 

but I get an error:

Table::write: Tag ToExpression in ToExpression[list[[1]]] is Protected.

Is there a way to get the right answer like:

Table[k1 + k2, {k1, 1, 2}, {k2, 1, 2}] 

{{2, 3}, {3, 4}}

$\endgroup$
3
  • 1
    $\begingroup$ Something like With[{kk1 = ToExpression@list[[1]], kk2 =ToExpression@list[[2]]}, Table[k1 + k2, {kk1, 1, 2}, {kk2, 1, 2}]]. See the Possible Issues section of Table :) $\endgroup$ Commented May 10, 2015 at 14:17
  • $\begingroup$ duplicates or closely related: 76917, 20719 $\endgroup$ Commented May 10, 2015 at 14:18
  • $\begingroup$ @Öskå Thank you very much! $\endgroup$ Commented May 10, 2015 at 14:22

1 Answer 1

6
$\begingroup$
Table[k1 + k2, Evaluate@{ToExpression[list[[1]]], 1, 2}, Evaluate@{ToExpression[list[[2]]], 1, 2}] (* {{2, 3}, {3, 4}} *) 

Or

Table[k1 + k2, {#, 1, 2}, {#2, 1, 2}] & @@ ToExpression[list] (* {{2, 3}, {3, 4}} *) 
$\endgroup$
1
  • $\begingroup$ Tank you very much for your excellent answer. $\endgroup$ Commented May 10, 2015 at 23:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.