Re: Variable amount of Buttons in Mathematica

*To*: mathgroup at smc.vnet.net*Subject*: [mg92885] Re: Variable amount of Buttons in Mathematica*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>*Date*: Fri, 17 Oct 2008 05:22:00 -0400 (EDT)*References*: <gcscrf$8l3$1@smc.vnet.net> <gcv77d$ds9$1@smc.vnet.net> <gd4djf$be8$1@smc.vnet.net>

magma wrote: > On Oct 13, 12:17 pm, "sjoerd.c.devr... at gmail.com" > <sjoerd.c.devr... at gmail.com> wrote: >> It can be done with: >> >> array = {1, 2, 3, 4, 5}; >> Row[Table[ >> With[{n = n}, >> Button[array[[n]], Print[n], ImageSize -> {50, 50}]], {n, 1, >> Length[array], 1}]] >> >> This uses the somewhate puzzling With[{n = n} construction, which >> replaces every occurance of the *variable* n (which doesn't make sense >> after the Table command has finished) with the succesive *values* it >> had during the table execution. >> >> Cheers -- Sjoerd > > Indeed Sjoerd, it is a very puzzling construct especially for a > newcomer like SDY. > I was amazed to see that all the "With" answers used n=n . > If this were a class exercise I would have suspected copying :-) > Then Eric mentioned that this was shown in the Button documentation > (they use i=i). > Ah, ah, so it was indeed copying :-) > > The point is: n=n is not necessary and frankly not recommended. > Mathematica syntax colouring shows the 2 n's with slightly different colours, > but it is hard to tell and not very illuminating for a newbie. > > I would suggest the following syntax: > > array = {1, 2, 3, 4, 5}; > Column[{Row[ > Table[With[{k = n}, > Button[array[[k]], Print[k], ImageSize -> {50, 50}]], {n, > Length[array]}]]}] > > > where I use With[{k=n}, ..... > Here the concept is clearer: each element in the table is built using > the "With-local" k. > In turn, k takes the current value of the "Table-looping" n. > > > > Perhaps some people are like me. I write such code without the 'With', and it doesn't work, so I use an expletive and add the 'With' without changing the name of the variable :) David Bailey http://www.dbaileyconsultancy.co.uk