Any idea how to get this to work?
y = {}; Table[Button[x, AppendTo[y, Evaluate[x]]], {x, 5}]
Result: Click [1
] , click [2
], get {6,6}
I'm trivializing the actual task, but the goal is to set what a button does inside a Map
or a Table
or ParallelTable
.
Please Help!
EDIT
Figured it out... Evaluate
works at first level only. Here, it's too deep. So I used ReplaceRule
:
Remove[sub]; y = {}; Table[Button[x, AppendTo[y, sub]] /. sub -> x, {x, 5}]
Replacement rules and pure functions offer concise alternatives to With
. For example:
y={}; Range[5] /. x_Integer :> Button[x, AppendTo[y, x]]
or
y = {}; Replace[Range[5], x_ :> Button[x, AppendTo[y, x]], {1}]
or
y = {}; Array[Button[#, AppendTo[y, #]] &, {5}]
or
y = {}; Button[#, AppendTo[y, #]] & /@ Range[5]
For another example comparing these techniques, see my post here, where they are applied to a problem of creating a list of pure functions with parameter embedded in their body (closures).