MathGroup Archive 2005

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

Search the Archive

Re: pattern matching all list elements but last


Well, I still did not conquer rules.  here is with Map and If:

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


On Feb 22, 2005, at 4:24 AM, Trevor Baca wrote:

> hi mathgroup folks,
> let's say ls looks like this:
>   ls = {{1}, {2,3}, {4,5,6}, {7,8,9,10}}
> is there an elegant way to match all integers *except the last
> (rightmost)* in each list and, say, add 100 to each?
>   ls /. <magic replacement rule goes here>
> or
>   ls //. <magic replacement rule goes here>
> to give {{1}, {102,3}, {104,105,6}, {107,108,109,10}}?
> this works:
>   Release[ ls //. {a___, b_Integer, c___, z_Integer} :> {a, Hold[b +
> 100], c, z} ]
> but is there a more elegant pattern, possibly one that avoids the
> Hold-Release trick?
> trevor.


  • Prev by Date: Re: pattern matching all list elements but last
  • Next by Date: Re: Pseudo-Random Numbers??
  • Previous by thread: Re: pattern matching all list elements but last
  • Next by thread: Re: pattern matching all list elements but last