Re: How to use Rules on Sequences?

• To: mathgroup at smc.vnet.net
• Subject: [mg29469] Re: How to use Rules on Sequences?
• From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
• Date: Thu, 21 Jun 2001 01:56:46 -0400 (EDT)
• Organization: Universitaet Leipzig
• References: <9gpo0n\$hd8\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,

In[]:=
L = {{a1, 2}, {a2, 1}, {a3, 3}};
L /. {a_, m_Integer} :> {a, m - 1} /. {_, 0} :> Sequence[]

Out[]={{a1, 1}, {a3, 2}}

And

Emil[1, 2, 3, 2, 0, a, b, c] //.
Emil[a___, b_, y_?Positive, b_, c___] :> Emil[a, b, 0, b, c]

gives

Emil[1, 2, 0, 0, 0, a, b, c]

Regards
Jens

Detlef Mueller wrote:
>
> Hello,
>
> I am faced to the following Problem:
>
> given a List of Pairs of the Form
>
> L = {{a1,m1},{a2,m2}, ... ,{ai,mi}}
>
> and a Number a, I want to replace the Pair
> {a, mj}, if it appears in the List by
> {a, mj-1}.
>
> This Job is done by
> L /. {{a,_m}->{a,m-1}}.
>
> Now I want to remove an Item of the Form
> {a,0} entirely from The List.
>
> Can this be done by applying rules (without
> leaving Null-Entrys), or is "Select" the only
> Way?
> (By the way: L is sorted with respect to the first Elements
> of the Sublists, this first elements are unic identifiers.
> Another Question is to replace an {a,_m} in L by {a,m+1},
> if it exists or else insert {a,1} in the propper
> Place - the inverse Operation of the above desribed
> one. Suerly not difficult to realize - but what might
> be the most efficient Method?)
>
> More general, I wonder, how to replace
> a Pattern in a Sequence - for example to change
> every occurence of "x_, y_?positive, x_" in a Sequence
> by "x,0,x".
>
> Say
>
> Emil[1,2,3,2,0,a,b,c]/.R ---> (Replacing ... 2,3,2 ... )
> Emil[1,2,0,2,0,a,b,c]/.R ---> (Replacing ... 0,2,0 ... )
> Emil[1,2,0,0,0,a,b,c]/.R      (No more changes).
>
> I Fail to write down a syntactically correct rule R to
> do this Job.
>
> Greetings
>   Detlef Mueller

```

• Prev by Date: How to use Rules on Sequences?
• Next by Date: Re: How to use Rules on Sequences?
• Previous by thread: How to use Rules on Sequences?
• Next by thread: Re: How to use Rules on Sequences?