I would like a function which returns replacement rules as some internal mathematica functions are doing and use the results in other functions. I could not find any information on that and I'm wondering if it is a good idea after all. I'm not sure how this is done in a good way.
Here is an example:
Clear[f, f2, H, imax, res]
f[imax_] :=
Block[{H}, H = Table[Random[], {i, 1, imax}];
Return[{H -> H, imax -> imax}]]
f[5]
with the result
{{0.855859, 0.656278, 0.793888, 0.275233, 0.751709} -> {0.855859,
0.656278, 0.793888, 0.275233, 0.751709}, 5 -> 5}
Yes this is obvious but I would like to use the name H internally and also in the returned replacement rule. So I do this instead:
f2[imax_] :=
Block[{H}, H = Table[Random[], {i, 1, imax}];
Return[{"H" -> H, "imax" -> imax}]]
res = Table[f2[imax], {imax, 5, 7}]
(*now i want to invesgate the result with imax=6*)
p = First @Flatten[Position[res, "imax" -> 6]]
res[[p]]
Head["H"] /. res[[p]]
with the result:
{{"H" -> {0.486493, 0.60306, 0.666644, 0.148913, 0.598069},
"imax" ->
5}, {"H" -> {0.873354, 0.98408, 0.0392209, 0.428918, 0.485521,
0.710918},
"imax" ->
6}, {"H" -> {0.198376, 0.385448, 0.438549, 0.818111, 0.314781,
0.533971, 0.955322}, "imax" -> 7}}
2
{"H" -> {0.873354, 0.98408, 0.0392209, 0.428918, 0.485521, 0.710918},
"imax" -> 6}
String
So the Problem is that Head["H"] /. res[[p]] results in a string and ListPlot["H"] /. res[[p]] would complain but plot the result. A good thing tough is that it is possible to search in the results and it is more flexible and readable than returning a List. So what is the best way to do this? So that I can use H and imax internally in the function and in the Return?
