FW: How to use Rules on Sequences?

• To: mathgroup at smc.vnet.net
• Subject: [mg29500] FW: [mg29452] How to use Rules on Sequences?
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
• Date: Sat, 23 Jun 2001 01:46:46 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```
-----Original Message-----
From: Wolf, Hartmut
To: mathgroup at smc.vnet.net
Subject: [mg29500] RE: [mg29452] How to use Rules on Sequences?

For my answer please see below

> -----Original Message-----
> From: Detlef Mueller [mailto:dmueller at mathematik.uni-kassel.de]
To: mathgroup at smc.vnet.net
> Sent: Wednesday, June 20, 2001 10:37 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg29500] [mg29452] How to use Rules on Sequences?
>
>
> 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
>
>
Since you already  got a row of answers here only some additional
suggestions:

If you want to keep your {key, count} pairs in sorted order, you may contain
them in a container with Attribute Orderless instead of in a List:

In[1]:= keys = {de, frosh, quakt, quark, quarrk}

In[2]:= Attributes[Olaf] = Orderless

In[3]:= L = Olaf @@ Transpose[{keys, Range[-1, 3]^2}]
Out[3]= Olaf[{de, 1}, {frosh, 0}, {quakt, 1}, {quark, 4}, {quarrk, 9}]

You got answers for decrement and to remove zero counts.
We remove frosh:

In[4]:= L = Replace[L , {_, 0} -> Sequence[], {1}]
Out[4]= Olaf[{de, 1}, {quakt, 1}, {quark, 4}, {quarrk, 9}]

This is an other proposal for counting up or insert (frosh in this case):
In[5]:=
froshup := L = Block[{matched = False},
Replace[L, { {frosh, n_} /; (matched = True) :> {frosh, n + 1},
Olaf[s___] /; ! matched :> Olaf[s, {frosh, 1}]}, {0, 1}]]

In[6]:= {L, froshup, froshup, froshup} // TableForm
Out[6]//TableForm=
Olaf[{de, 1}, {quakt, 1}, {quark, 4}, {quarrk, 9}]
Olaf[{de, 1}, {frosh, 1}, {quakt, 1}, {quark, 4}, {quarrk, 9}]
Olaf[{de, 1}, {frosh, 2}, {quakt, 1}, {quark, 4}, {quarrk, 9}]
Olaf[{de, 1}, {frosh, 3}, {quakt, 1}, {quark, 4}, {quarrk, 9}]

However I wouldn't follow that approach. It will become overly slow if you
keep many counters. Instead use Mathematica symbol store (hash table). Our
"container" is now a symbol, say 'Ede'.

In[16]:= Ede[a_] := Ede[a] = 0

This general rule automatically creates a counter (with key a), if needed.

In[17]:= inc[a_] := Ede[a] += 1
In[18]:=
dec[a_] := If[(Ede[a] -= 1) <= 0, Unset[Ede[a]]; 0, Ede[a]]

In[23]:= inc /@ Table[frosh, {3}]
Out[23]= {1, 2, 3}

In[24]:= dec /@ Table[frosh, {4}]
Out[24]= {2, 1, 0, 0}

In[25]:= clr := (ClearAll[Ede]; Ede[a_] := Ede[a] = 0)

To clear all counters.

In[26]:= clr

In[27]:=
MapThread [inc /@ Table[#1, {#2}] &, {keys, Range[-1, 3]^2}]

This just sets up the same content as in Out[3] above

In[28]:= DownValues[Ede] // TableForm
Out[28]//TableForm=
HoldPattern[Ede[de]] :> 1
HoldPattern[Ede[quakt]] :> 1
HoldPattern[Ede[quark]] :> 4
HoldPattern[Ede[quarrk]] :> 9
HoldPattern[Ede[a_]] :> (Ede[a] = 0)

No frosh present, however if you ask for it, it shows up

In[29]:= Ede[frosh]
Out[29]= 0

Of course you may ask for quark

In[30]:= Ede[quark]
Out[30]= 4

-- Hartmut Wolf

```

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