Re: Sifting a list sans loop
Re: Sifting a list sans loop
- Subject: Re: Sifting a list sans loop
- From: Arthur J Knapp <email@hidden>
- Date: Mon, 28 Jan 2002 11:14:37 -0500
>
Date: Mon, 28 Jan 2002 11:28:57 +0100
>
From: "Serge Belleudy-d'Espinose" <email@hidden>
>
Subject: Re: Sifting a list sans loop
>
At 22:51 -0500 26/01/02, Victor Yee wrote:
>
> set theList to {1, 1, {"2"}, "3", true, {4}, "3", 1}
>
> plain text of theList
>
> --> {"3", "3"}
>
!!!
>
>
This is the second time in a week I get to see such magic code everybody seems
>
to know but I can't find any written reference to in the ASLG or elsewhere.
There is a secret society called the Illuminatus, they keep the knowledge
of the ancients...
;-)
You will find many mentions of this syntax in the list archives, both
Apple's and MacScrpt.
>
I wonder, how do you guys know? being old-timer? word of mouth? or is there a
>
repository only the blessed ones know- and the side one, how do I become bless
I am surprised that the ASLG doesn't mention this. Using the "every class
of list" syntax is one of the more useful things that you can do with a list
in vanilla AppleScript. It is commonly used in the class-delete method:
set myStrings to {"Hello", "World", "Goodbye", "Sky"}
set myStrings to DeleteStringsAt(myStrings, {2, 4})
--
--> {"Hello", "Goodbye"}
on DeleteStringsAt(string_list, index_list)
repeat with x in index_list
set string_list's item x to 0
end repeat
return every string of string_list
end DeleteStringsAt
The "traditional" way to delete an item from a list is to
break it apart and then put it back together, minus the
deleted item:
on DeleteItemAt( item_list, delete_index )
if ( delete_index = 1 ) then
return item_list's rest
else if ( delete_index = item_list's length ) then
return item_list's items 1 thru -2
else
return ,
( item_list's items 1 thru ( delete_index - 1 ) ) & ,
( item_list's items ( delete_index + 1 ) thru -1 )
end if
end DeleteItemAt
set myStrings to { "Hello", "World", "Goodbye", "Sky" }
set myStrings to DeleteItemAt( myStrings, 4 )
--
--> { "Hello", "World", "Goodbye" }
set myStrings to DeleteItemAt( myStrings, 2 )
--
--> { "Hello", "Goodbye" }
For deleting any single item, there is no real difference between
the two methods, but the class-delete method can be faster and/or
more convenient in certain situations.
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.eremita.demon.co.uk/scripting/applescript/>
on error number -128
end try
}