Re: ReplacePart in an If[] construct?
- To: mathgroup at smc.vnet.net
- Subject: [mg67830] Re: [mg67816] ReplacePart in an If[] construct?
- From: gardyloo <gardyloo at mail.wsu.edu>
- Date: Sun, 9 Jul 2006 04:51:07 -0400 (EDT)
- References: <22230202.1152366486517.JavaMail.root@eastrmwml07.mgt.cox.net>
- Sender: owner-wri-mathgroup at wolfram.com
Thank you, Mr. Hanlon. That's just the solution I came up with, but now
I understand why my first attempt didn't work!
C.O.
Bob Hanlon wrote:
> You threw away the intermediate result since you are not actually changing the input list. Nest the ReplacedParts
>
> testList={{3,an,example,list},{4,another,example,list}};
>
> (If[(ListQ[#1]&&First[#1] == 4),
> ReplacePart[
> ReplacePart[#1,replaced!,2],
> replaced!,3],
> #1 (*otherwise*)]&)/@testList
>
> {{3, an, example, list}, {4, replaced!, replaced!, list}}
>
>
> Bob Hanlon
>
> ---- gardyloo <gardyloo at mail.wsu.edu> wrote:
>
>> Hi, all,
>>
>> It's probably that I'm too tired, but I have a question about using
>> ReplacePart in an If[] construct. I've made up a (very) minimal example:
>>
>> In[1]:=
>> testList = {{3, an, example, list},
>> {4, another, example, list}}
>>
>> Out[1]=
>> {{3, an, example, list}, {4, another, example, list}}
>>
>> 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
>> plenty of other ways of doing this, but this seemed the most
>> straightforward, and I can't wrap my head around it, for some reason.
>>
>>
>>
>> --
>> ==========================================================
>> Curtis Osterhoudt
>> gardyloo at mail.remove_this.wsu.and_this.edu
>> PGP Key ID: 0x088E6D7A
>> Please avoid sending me Word or PowerPoint attachments
>> See http://www.gnu.org/philosophy/no-word-attachments.html
>> ==========================================================
>>
>>
>
>
>
--
==========================================================
Curtis Osterhoudt
gardyloo at mail.remove_this.wsu.and_this.edu
PGP Key ID: 0x088E6D7A
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================
--------------010602010104000501000303
name="gardyloo.vcf"
filename="gardyloo.vcf"
YmVnaW46dmNhcmQNCmZuOkN1cnRpcyAgT3N0ZXJob3VkdA0KbjpPc3RlcmhvdWR0O0N1cnRp
cyANCmVtYWlsO2ludGVybmV0OmdhcmR5bG9vQG1haWwucmVtb3ZlX3RoaXMud3N1LmFuZF90
aGlzLmVkdQ0KdGVsO3dvcms6NTA5LjMzNS40OTQ2DQpub3RlOlBHUCBLZXkgSUQ6IDB4MDg4
RTZEN0ENCngtbW96aWxsYS1odG1sOkZBTFNFDQp2ZXJzaW9uOjIuMQ0KZW5kOnZjYXJkDQoN
Cg==
--------------010602010104000501000303--