# help with dynamic input

I'm trying to build a pop up window with i lines. Each line would read:

Number of neurons in layer 1:
Number of neurons in layer 2:
etc until layer i

The user would enter a numeric value on each line. That value would be recorded as d,d,...,d[i].

The solution I've come up with results in Mathematica assigning the input from any line to d[i] (for example if there are 5 lines, it assign inputs from line 1, 2..5 all to d). I understand why it's happening. I just can't think of how I'm suppose to achieve what I want.

Here's my code:

a = Table[
List[StringJoin[{"Number of neurons in layer ", ToString[i], ":"}],
InputField[Dynamic[d[i]], Number]], {i, 3}];
AppendTo[a, {CancelButton[], DefaultButton[DialogReturn[]]}];
CreateDialog[Grid[a, Spacings -> {1, Automatic}, Alignment -> Left], Modal -> True];


Any suggestions? Thanks.

• You need to change the first argument (List[...]) in Table to With[{i=i},List[...]]. Please check this answer to a related question.
– kglr
Aug 28, 2012 at 23:20

Simple fix. Put With[{i=i}] to get the indexing in the dynamic right:

a = Table[List[
StringJoin[{"Number of neurons in layer ", ToString[i], ":"}],
With[{i = i}, InputField[Dynamic[d[i]], Number]]], {i, 3}];
AppendTo[a, {CancelButton[], DefaultButton[DialogReturn[]]}];

CreateDialog[Grid[a, Spacings -> {1, Automatic}, Alignment -> Left], Modal -> True];


On a sidenote, When I paste your code into Mathematica the i in InputField[Dynamic[d[i]], Number] turns red. If you highlight such a red character and go to the menu "help/why the coloring" it actually tells you that the order of evaluation conflicts.

I have added some more details on why this problem occurs, and why the solution works to a previous related question: Generating dynamic Inputfields