MathGroup Archive 2005

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

Search the Archive

Re: match 1, replace 2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56259] Re: [mg56199] match 1, replace 2
  • From: János <janos.lobb at yale.edu>
  • Date: Wed, 20 Apr 2005 05:31:59 -0400 (EDT)
  • References: <200504190854.EAA02514@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Apr 19, 2005, at 4:54 AM, Trevor Baca wrote:

> hi mathgroup,
>
> what rule takes
>
> ls = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}}
>
> to
>
>       {{101, -1, 102, -1, 3}, {104, -1, 5}, {106, -1, 107, -1, 108,  
> -1,
> 9}}
>
> ie, matches all integers but the last in each list and then replaces
> each matched integer n with the 2-element sequence Sequence[100 + n,
> -1]?
>
> trevor.

In[1]:=
ls = {{1}, {2, 3}, {4, 5, 6},
    {7, 8, 9, 10}}
Out[1]=
{{1}, {2, 3}, {4, 5, 6},
   {7, 8, 9, 10}}

In[4]:=
Table[Flatten[
    (If[IntegerQ[#1] &&
        #1 =!= Last[ls[[i]]],
       {Sequence[#1 + 100],
        -1}, #1] & ) /@
     ls[[i]]], {i, 1,
    Length[ls]}]
Out[4]=
{{1}, {102, -1, 3},
   {104, -1, 105, -1, 6},
   {107, -1, 108, -1, 109, -1,
    10}}

János


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Re: match 1, replace 2
  • Next by Date: Re: assigning functions
  • Previous by thread: Re: match 1, replace 2
  • Next by thread: Re: match 1, replace 2