Re: pattern matching all list elements but last
- To: mathgroup at smc.vnet.net
- Subject: [mg54598] Re: [mg54539] pattern matching all list elements but last
- From: yehuda ben-shimol <bsyehuda at gmail.com>
- Date: Thu, 24 Feb 2005 03:21:03 -0500 (EST)
- References: <200502220924.EAA10849@smc.vnet.net>
- Reply-to: yehuda ben-shimol <bsyehuda at gmail.com>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
Although your question was focused on the pattern matching style I
would like to not ethat you can also use the functional programming
style
e.g.
ls = {{1}, {2,3}, {4,5,6}, {7,8,9,10}};
Flatten[{100 + Most[#], Last[#]}] & /@ ls
and get the same result
yehuda
On Tue, 22 Feb 2005 04:24:35 -0500 (EST), Trevor Baca
<trevorbaca at gmail.com> 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.
>
>
- References:
- pattern matching all list elements but last
- From: "Trevor Baca" <trevorbaca@gmail.com>
- pattern matching all list elements but last