1
$\begingroup$

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]] == ""],                   
     (symbol[[i]] = "Please Enter"),(symbol[[i]] ="Right")                                                                           
  ]  ]}] 

I am calling this function here.

DynamicModule[
{s = {}},
 For[i = 1, i <= 3, i++,
     With[{i = i},AppendTo[s, logicalInputField[i, {50, 20}, 3, True,{"", "", ""}, {"Please Enter", "Please Enter", "Please Enter"}]]]];
     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.

$\endgroup$
4
  • $\begingroup$ I don't understand the code very well… but I think a semicolon is needed before Panel. $\endgroup$
    – xzczd
    Sep 11, 2012 at 7:05
  • $\begingroup$ @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. $\endgroup$
    – Jennifer
    Sep 11, 2012 at 7:22
  • $\begingroup$ 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. $\endgroup$
    – xzczd
    Sep 11, 2012 at 9:08
  • $\begingroup$ @xzczd: yes. It gives the value when both the arguments tx and symbol are removed from the defined functions. $\endgroup$
    – Jennifer
    Sep 11, 2012 at 9:09

1 Answer 1

3
$\begingroup$

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]] == ""], 
           (sysy[[i]] ="Please Enter"), (sysy[[i]] ="Right")]]}])

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_] := 
 DynamicModule[{tx = txa}, 
  Column[{InputField[Dynamic[tx[[i]]], Number, ImageSize -> fieldSize, 
     Enabled -> enable, ContinuousAction -> True], 
    Dynamic[If[TrueQ[tx[[i]] == ""], "Please Enter", "Right"]]}]]

Panel[Row[Table[
   logicalInputField[i, {50, 20}, 3, True, {"", "", ""}], {i, 3}]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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