I am using variables of the form $a[i]$, e.g. $(a[1],a[2],a[3],...)$. When I pass them to a function I need to determine which one they are, in other words, what the value of $i$ is.
Specifically, I need to do something like this:
a[1]=2.1; a[2]=3.5;
Test[x_]:=Block[{i}, *Some how set i=index value of a[i]*;
Return[{i,a[i]}];
];
So that
Test[a[1]]
returns
{1,2.1}
and
Test[a[2]]
returns
{2,3.5}
fn[a[n_]] := Row@{"this one is ", n}
. Somehow I think that you might mean something else however. Perhaps you are having evaluation problems, e.g. there is an existing definition likea[1] = 3.2
? $\endgroup$ – Mr.Wizard Aug 1 '16 at 0:10a[1]
ora[2]
? If soSetAttributes[Test, HoldAll]; Test[a[i_]] := i
would work. But it won't work in all applications. E.g.var = a[2]; Test[var]
. $\endgroup$ – Michael E2 Aug 1 '16 at 0:29{i, a[i]}
is shorter thanTest[a[i]]
. $\endgroup$ – Michael E2 Aug 1 '16 at 0:58a
and the index (1
or2
) separate to begin with. In fact this is one of the major advantages of using "indexed objects" like this, e.g. (32202). You might define yourTest
function something likeTest[s_Symbol, i_] := {i, s[i]}
then call it withTest[a, 1]
. This does not require any Hold attributes. $\endgroup$ – Mr.Wizard Aug 1 '16 at 1:21test[s_Symbol][i_] := {i, s[i]}
which would allowtest[a] /@ {1, 2}
. Finally please consider not starting user function names with capital letters as there is risk of collision with built-ins, either now or in a future version. $\endgroup$ – Mr.Wizard Aug 1 '16 at 1:24