1
$\begingroup$

Possible Duplicate:
Best way to extract values from a list of rules?

If I have a vector v = {x->1.03, x-> 2.01, .... }, and I want to use an element in the list, I can get an element as v[[1]] = x-> 1.03 but suppose I want to get rid of the arrow?

Thanks for any suggestions.

$\endgroup$
3
  • $\begingroup$ x /. v[[1]] ? $\endgroup$
    – cormullion
    Jan 28, 2013 at 11:15
  • 1
    $\begingroup$ there's no hurry :) - the longer you wait, the better the answers become! $\endgroup$
    – cormullion
    Jan 28, 2013 at 11:17
  • $\begingroup$ @rm -rf oy, good catch ;-) $\endgroup$
    – Yves Klett
    Jan 28, 2013 at 15:27

3 Answers 3

7
$\begingroup$

Since ReplaceAll (/.) will only return the first match, i.e.

v = {x -> 1.03, x -> 2.01, x -> 3.02};
w = x /. v

1.03

you could extract all the values considering the FullForm:

FullForm[v]

List[Rule[x, 1.03], Rule[x, 2.01], Rule[x,3.02`]]

w = v[[All, 2]]

{1.03, 2.01, 3.02}

$\endgroup$
1
  • $\begingroup$ Table[x /. v[[i]], {i, 1, n}] gave all of them too. $\endgroup$
    – daniel
    Jan 28, 2013 at 13:22
6
$\begingroup$

I reckon that by "get rid of the arrow" you mean you want to access only the numbers from v. You can achieve that by using

x/.v[[1]]

This applies the first replacement rule given in v to x. You might want to check out Rule and ReplaceAll in the documentation.

$\endgroup$
1
  • 1
    $\begingroup$ achive/.achive->achieve... $\endgroup$
    – einbandi
    Jan 28, 2013 at 11:21
6
$\begingroup$

Not really surprising, but educational: The syntax looks confusing to beginners, but it is very well worth your time to get acquainted with animals like Replace(/.), Rule(->) or Map(/@).

x /. # & /@ {x -> 10, x -> 40}

(*{10, 40} *)

This also works a bit more literally (if not efficently) following your request:

{x -> 10, x -> 40} /. (x -> y_) -> y

(* {10, 40} *)
$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.