# Using HoldFirst but not wishing to hold everything

I have a DynamicModule in which I go through some data analysis and keep or reject the results. Here is a toy example of what I am doing.

ClearAll[f];
SetAttributes[f, HoldFirst];
f[{ip_, op_}] := DynamicModule[{a, b},
a = ip;
Column[{
Dynamic[b = Total[a]],
Row[{
Button["keep", AppendTo[op, {ToString[Unevaluated[ip]], b}]],
Button["reject", AppendTo[op, {ToString[Unevaluated[ip]], Null}]]
}]
}]
]


I make 10 sets of data

data = Table[RandomReal[{-1, 1}, 10], {10}];


I now run my module on the first set using

op = {};
f[{data[[1]], op}]


Which gives me

When I look at my output I have

op


Which is what I want. This output records the data set and the result. If I do this again with all the data using a table I can still proceed

op = {};
Column[Table[f[{data[[n]], op}], {n, Length[data]}]]


I press my buttons. However when I now look at my output all the records are data[[n]], data[[n]], data[[n]]... instead of data[[1]], data[[2]], data[[3]]...

op // TableForm


What I want is this

How can I make the n in the output of data[[n]] change to reflect the input number? Thanks for any suggestions.

• With can inject into held expressions: Column[Table[With[{n = n}, f[{data[[n]], op}]], {n, Length[data]}]] – Kuba Sep 18 '14 at 18:56
• @Kuba A good workaround. Many thanks. Pity I can't include this in the module somehow... – Hugh Sep 19 '14 at 8:55

As Kuba suggested in his comment, With is helpful for accomplishing what you ask for.

ClearAll[f];
SetAttributes[f, HoldFirst];
f[ip_] :=
Module[{b},
Column[{b = Total[ip],
Row[{
Button["keep", AppendTo[op, {ToString[Unevaluated[ip]], b}]],
Button["reject", AppendTo[op, {ToString[Unevaluated[ip]], Null}]]}]}]]

SeedRandom[42]; data = RandomReal[{-1, 1}, {4, 10}];
op = {};
Column[Table[With[{n = n}, f[data[[n]]]], {n, Length[data]}]]


op

{
{"data[[1]]", -1.47076},
{"data[[2]]", Null},
{"data[[3]]", 3.35806},
{"data[[4]]", 2.33047}
}


To reduce the size of my answer, I have reduced the data to four lists of ten real numbers and omitted the formatting of the output. I have also done some simplification on your definition of f. In particular, I eliminated the use of dynamic constructs as they are not needed for what you are doing.

• Thanks for this. I think this solves my problem although it makes using the module more complicated. The simplifications to my module are also worth considering. This could leave to another question... – Hugh Sep 19 '14 at 8:55