If I have a list of rules associated with indexed variables like:
solution = {a[1] -> 1, a[2] -> 10, a[3] -> 100}
I would like to be able to extract all of the values associated with the rules. Although this problem is simple with a small number of variables, I am not sure how to generalize it.
For instance, I can use pattern matching to obtain 1 and 10, respectively:
a[1]/.solution
a[2]/.solution
However, I can't seem to generalize this to extract a[n] values from a list of rules. What is proper pattern to do this?
Array[a, 3]myself, FWIW. You might want to address the possibility of:>rules in yourCasesmethods. – Mr.Wizard Aug 26 '13 at 21:27solution = {a[1] -> 1, a[2] :> $var, a[3] -> 100}-- it would be good to at least mention that this won't be handled by the patternRule[a[_], x_] :> x. By the way I notice you're usingHoldPattern; if it is only for grouping you could use parentheses as well. – Mr.Wizard Aug 26 '13 at 21:45solution /. (a[_] -> x_) :> x=> {1, 10, 100}? – user1066 Aug 26 '13 at 23:26Casesbut simpler in form, you may add this example as an answer too :) – Kuba Aug 26 '13 at 23:39