• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sifting a list sans loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
}


  • Follow-Ups:
    • Re: Sifting a list sans loop
      • From: John W Baxter <email@hidden>
  • Prev by Date: Re: OS X test for Class Folder...continues
  • Next by Date: can I make a list of records?
  • Previous by thread: Re: Sifting a list sans loop
  • Next by thread: Re: Sifting a list sans loop
  • Index(es):
    • Date
    • Thread