0
$\begingroup$

I'm trying to do something like this:

test = {x, y};
With[{test[[1]] = 3}, x/y]

However, the following error is printed:

With::lvset: "Local variable specification {test[[1]]=3} contains test[[1]]=3, which is an assignment to test[[1]]; only assignments to symbols are allowed"

I'm assuming this is because Mathematica treats the list elements as expressions and not as symbols correct? Setting x directly works, but I need the index for a loop functionality. Any hint on how to force Mathematica to interpret test[[1]] as a symbol?

$\endgroup$
7
  • 1
    $\begingroup$ You could use a rule replacement x /. x -> 3 $\endgroup$ Commented May 11, 2015 at 11:45
  • 3
    $\begingroup$ With[{# = 3}, x/y] &[test[[1]]] if x has no value. $\endgroup$
    – Kuba
    Commented May 11, 2015 at 11:49
  • 3
    $\begingroup$ With has attribute HoldAll hence test[[1]] isn't evaluated to x prior to trying to make the assignment. $\endgroup$
    – Bob Hanlon
    Commented May 11, 2015 at 13:12
  • 1
    $\begingroup$ @BobHanlon not only With is the problem. Set is HoldFirst too. $\endgroup$
    – Kuba
    Commented May 11, 2015 at 13:46
  • 1
    $\begingroup$ Ok, this is complex issue, I think you should read this 28610 (probably duplicate) topic and linked/related ones. $\endgroup$
    – Kuba
    Commented May 11, 2015 at 14:01

0

Browse other questions tagged or ask your own question.