5

I want to be able to assign values to a list element inside a module. For example if I have:

```
testscan[listin_] :=
Module[{zz},
Do[listin[[1]] = zz; Print[{zz, listin[[1]]}], {zz, 1, 2, 1}]]
```

Given:

```
testscan[{a, 1, 3, 1}]
```

Returns:

```
{1,a}
{2,a}
```

What I want it to return:

```
{1,1}
{2,2}
```

So its not just the output I care about, I want 'a' in this case to be able to reassigned to anything. Any ideas? Thanks a lot for your time.

LH- it is indeed a that I want to modify. The page you linked to is on the right track, but doesn't quite give me what I need yet. For example using your setPart function:

in:

```
foo = {a, 1, 3, 1};
setPart[foo, 1, xxx];
foo
```

out:

```
{xxx, 1, 3, 1}
```

But then for:

IN:

```
Clear[listin];
testscan[listin_] :=
Module[{}, setPart[listin, 1, \[Pi]]; Print[{listin[[1]], listin}]]
testscan[{a, 1}]
```

out:

```
{a,{a,1}}
```

So setPart doesn't seem to work inside modules as the way I have it. Can you offer my any further assistance? Again, thank you so much for your time.

Leonid - I am fairly new to Mathematica so I wasn't sure about these subtleties, but now its pretty clear that indeed I am just trying to find workarounds instead of a proper solution. The 'true' problem I am working on is trying to scan a parameter space of varying numbers of parameters subject to some constraints (I am interested in any number of constraints just out of curiosity, but in reality no more than 2 constraints in the actual). So it would look something like :

TestScan[c1,...,cm,{list1,....listn}]

Where the ci are constraints and the list_i's take the form {x_i,x_i_initial, x_i_final, x_i_increment} that will be fed to a Do loop.

For example I might have a constraint like x^2+y^2+z^2=1, I want to scan over values of y and z, solve for the corresponding values of x, then store all the possible solutions in a list. By putting the variables I want to scan over in an array it seems easier since then I can have Mathematica see the size of the array, and run the proper number of Do loops. Obviously if I just put the variables in a function like

TestScan[c1,...,cm,x_i,x_i_initial, x_i_final, x_i_increment,....]

it works, but I would have to change the code every time I change the number of variables in the constraints. So this to me boils down to my earlier question of how to make Mathematica see a part of a list as a variable that I can then increment in a Do loop inside a Module.Do you have any advice how I could achieve this without such work-arounds, and hopefully without bothering you any more? Thanks a ton for your time. DB

Addressing your edit: this has nothing to do with

`Module`

. If you want to modify list elements persistently, you have to store the list in a variable, as I mentioned (instead, you again pass the list directly, and ignore my advice on`Hold`

-attribute for`testscan`

). Generally, what you try to achieve is going against the semantics of Mathematica regarding assignments/part modifications. Therefore, the work-arounds will likely be complex and lead to more questions. I would rather give a larger context of your real problem - it is more than likely that better alternative solutions exist. – Leonid Shifrin – 2012-03-21T10:41:41.037