Re: match 1, replace 2
- To: mathgroup at smc.vnet.net
- Subject: [mg56247] Re: [mg56199] match 1, replace 2
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 20 Apr 2005 05:30:43 -0400 (EDT)
- References: <200504190854.EAA02514@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 19 Apr 2005, at 17:54, 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.
>
>
Mapping this function
Function[l, MapIndexed[If[First[#2] < Length[ l], Sequence @@ {100 +
#1, -1}, #1] &, l]]
onto your list will do it:
Function[l,MapIndexed[
If[First[#2]<Length[l],Sequence@@{100+#1,-1},#1]&,l]]/@{{1,2,3},{4,
5},{6,7,8,9}}
{{101,-1,102,-1,3},{104,-1,5},{106,-1,107,-1,108,-1,9}}
Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/andrzej/index.html
http://www.mimuw.edu.pl/~akoz/
- References:
- match 1, replace 2
- From: "Trevor Baca" <trevorbaca@gmail.com>
- match 1, replace 2