# Calling part of a variable in a user defined function

I have defined a function

logicalInputField[i_, fieldSize_, numberOfFields_, enable_,tx_,symbol_] :=
Column[{InputField[Dynamic[tx[[i]]], Number, ImageSize -> fieldSize,
Enabled -> enable, ContinuousAction -> True],
Dynamic[If[TrueQ[tx[[i]] == ""],
]  ]}]


I am calling this function here.

DynamicModule[
{s = {}},
For[i = 1, i <= 3, i++,
Panel[ Row[s]]]


I want to ask whenever I am not giving tx and symbol as an argument at the time of defining function. and then calling it, it works fine. But, as soon as I enter some value in InputField , it gives error message

Set::setps: {, ,} in the part assignment is not a symbol.


Why? How can I call the Part of a variable from the user defined function.

• I don't understand the code very well… but I think a semicolon is needed before Panel. Sep 11, 2012 at 7:05
• @xzczd: This code is just a sample but I want to ask how can I call the Part of the list. Here symbol and tx are the lists. So, how can I call these variables from the function. I guess this is a wrong way of calling. Therefore, I am getting error. Sep 11, 2012 at 7:22
• I think I get one of the point: If[TrueQ[tx[[i]] == ""], (symbol[[i]] = "Please Enter"),(symbol[[i]] ="Right") ]doesn't give back a value.symbol[[i]] =should be deleted here. Sep 11, 2012 at 9:08
• @xzczd: yes. It gives the value when both the arguments tx and symbol are removed from the defined functions. Sep 11, 2012 at 9:09

The problem can be boiled down to the following:

In programming language like python I can define a function like this:

def test(x):
x[0]=2
return x

test([3])
#The out put is [2]


But in Mathematica

 f[x_] := (x[[1]] = 2; x)
f[{3}]


outputs {3} with the warning Set::setps, why? I expect {2} to be the output!

It's because, unlike in language like python, once a function is called in Mathematica, the arguments in function body will be immediately replaced by the expressions sent into the function as arguments. This can be proved by:

f[x_] := (y = x; Print@Hold@y; Print@Hold@x)
f[{2}]

(*=> Hold[y]

Hold[{2}]*)


The problem can be solved by using intermediate variables in the function, here is a quick fix:

logicalInputField[i_, fieldSize_, numberOfFields_, enable_, tx_, symbol_] :=

(txtx = tx; sysy = symbol;

Column[{InputField[Dynamic[txtx[[i]]], Number, ImageSize -> fieldSize,
Enabled -> enable, ContinuousAction -> True],
Dynamic[If[TrueQ[txtx[[i]] == ""],

This isn't the end. By avoiding global intermediate variables, For loop and unnecessary symbol, we can make the code sample cleaner:
logicalInputField[i_, fieldSize_, numberOfFields_, enable_, txa_] :=