Re: ReplacePart in an If[] construct?
- To: mathgroup at smc.vnet.net
- Subject: [mg67832] Re: ReplacePart in an If[] construct?
- From: kalymereau at yahoo.fr
- Date: Sun, 9 Jul 2006 04:51:11 -0400 (EDT)
- References: <e8nttf$kcf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi
gardyloo a écrit :
> In[2]:=
> (If[ (ListQ[#1] && First[#1] == 4),
> ReplacePart[#1, replaced!, 3];
> ReplacePart[#1, replaced!, 2],
> #1 (*otherwise*)
> ] & ) /@ testList
>
> Out[2]=
> {{3, an, example, list}, {4, replaced!, example, list}}
>
>
> Can someone tell me why BOTH the second and third positions in
> the second element in testList aren't turned to "replaced!" ? I have
With this syntax you should write
(If[ (ListQ[#1] && First[#1] == 4),
tmp=ReplacePart[#1, replaced!, 3];
ReplacePart[tmp, replaced!, 2],
#1 (*otherwise*)
] & ) /@ testList
But why not simply use pattern matching:
testList/.{4,_,_,z___}:>{4,replaced!,replaced!,z}
?
- Follow-Ups:
- Re: Re: ReplacePart in an If[] construct?
- From: gardyloo <gardyloo@mail.wsu.edu>
- Re: Re: ReplacePart in an If[] construct?