Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replacement Rule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32076] Re: [mg32064] Replacement Rule
  • From: BobHanlon at aol.com
  • Date: Sat, 22 Dec 2001 04:22:49 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 12/21/01 4:17:57 AM, bghiggins at ucdavis.edu writes:

>I have the following list
>
>l1={{1,2},{3,4},{5,6}};
>
>and then I operate on it with the following pattern/replacement rule
>
>In[3]:= l1 /. {x_, y_} -> x
>
>Out[3]= {1, 3, 5}
>
>This is what I would expect. Now consider the following list
>
>In[4]:= l2 = {{3, 4}, {5, 6}};
>
>Then I use the same pattern/replacement rule 
>
>In[5]:=l2 /. {x_, y_} -> x
>
>Out[5]= {3, 4}
>
>I was hoping to get
>
>{3,5}
>
>What am I missing? The FullForm of l2 is basically the same structure
>as l1, as far as I can tell....

In your first case the outer list has three elements and does not match 
the pattern so the rule is applied to each of the individual element lists.  
In the second case, the outer list has two elements and matches the 
pattern.  Consequently, the replacement rule is applied to the outer list.

One solution is to restrict the elements of the list in the replacement 
rule to being numeric

l1 = {{1,2},{3,4},{5,6}};
l2 = {{3,4},{5,6}};
rr1 = {x_?NumericQ,y_?NumericQ}->x;

l1 /. rr1

{1, 3, 5}

l2 /. rr1

{3, 5}


Bob Hanlon
Chantilly, VA  USA


  • Prev by Date: Re: Replacement Rule
  • Next by Date: RE: Replacement Rule
  • Previous by thread: Re: Replacement Rule
  • Next by thread: RE: Replacement Rule