Sunday, September 15, 2019

#1 2009-06-22 11:16:22 pm

mleonti
Member
Registered: 2008-10-20
Posts: 154

deleting items from a list in a repeat

My aim is to eliminate each item, one at a time, and represent the remaining ones until no more remain.
This works however I have thousands of items in the list and it is slow.
Is there a way to get the position of the item (its number in the list) and then just delete that item?
That would make it so much faster.
Thanks.

Applescript:

set myList to {"a", "b", "c", "d"}
set myParsedList to {}
repeat while myList is not {}
   set usrChc to (choose from list myList with prompt "Delete from list" OK button name "Delete")
   if usrChc is false then return -- user cancelled
   repeat with thisItem in myList
       if (thisItem as text) is not (usrChc as text) then set end of myParsedList to thisItem
   end repeat
   set myList to myParsedList
   set myParsedList to {}
end repeat

Offline

 

#2 2009-06-23 12:30:16 am

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: deleting items from a list in a repeat

Perhaps

Applescript:

set mylist to {"able", "baker", "charlie", "delta", "baker", "able"}

set tid to AppleScript's text item delimiters

repeat while mylist is not {}
   set usrChc to (choose from list mylist with prompt "Delete from list" OK button name "Delete")
   if usrChc is false then -- user cancelled
       set AppleScript's text item delimiters to tid
       return
   end if
   
   set AppleScript's text item delimiters to {ASCII character 5}
   set myString to mylist as string
   
   set AppleScript's text item delimiters to {usrChc & (ASCII character 5)}
   set mylist to text items of myString
   
   set AppleScript's text item delimiters to {""}
   set myString to mylist as string
   
   set AppleScript's text item delimiters to {ASCII character 5}
   set mylist to text items of myString
   
end repeat

set AppleScript's text item delimiters to tid

Last edited by mikerickson (2009-06-23 12:36:06 am)

Offline

 

#3 2009-06-23 07:09:59 am

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: deleting items from a list in a repeat

This is a bit clunkier, but more robust

Applescript:

set tid to AppleScript's text item delimiters

set myList to {"a", "abe", "a", "b", "a", "alice", "a"}
repeat while myList is not {}
   set usrChc to (choose from list myList with prompt "Delete from list" OK button name "Delete")
   if usrChc is false then -- user cancelled
       set AppleScript's text item delimiters to tid
       return
   end if
   set myDelimiter to ASCII character 5
   set myOtherDelimiter to ASCII character 11
   
   set myDelimiter2 to myDelimiter & myDelimiter
   set myotherDelimiter2 to myOtherDelimiter & myOtherDelimiter
   
   set AppleScript's text item delimiters to {myOtherDelimiter & myDelimiter2 & myOtherDelimiter}
   set mystring to myOtherDelimiter & (myList as string) & myOtherDelimiter
   
   set AppleScript's text item delimiters to {myOtherDelimiter & usrChc & myOtherDelimiter}
   set myList to text items of mystring
   
   set AppleScript's text item delimiters to {""}
   set mystring to myList as string
   
   repeat until not (mystring contains myDelimiter)
       set AppleScript's text item delimiters to {myDelimiter}
       set myList to text items of mystring
       
       set AppleScript's text item delimiters to {""}
       set mystring to myList as string
   end repeat
   
   try
       set mystring to characters 2 thru -2 of mystring as string
   end try
   set AppleScript's text item delimiters to {myotherDelimiter2}
   
   set myList to text items of mystring
end repeat
set AppleScript's text item delimiters to tid

Offline

 

#4 2009-06-23 07:48:11 pm

mleonti
Member
Registered: 2008-10-20
Posts: 154

Re: deleting items from a list in a repeat

Thanks mikerickson,

I tested and I am sorry to report that with 29456 files in the list it still takes too much time.

I was hoping that you would be able to get the item number of the choice in the list so that you could simply say delete item itmNr and be done with it.

I am now working on cutting down the list into smaller lists (up to 2000 items it is not so bad) and presenting them one at a time until they are all empty.

Offline

 

#5 2009-06-24 07:54:21 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: deleting items from a list in a repeat

The fastest way I know of to traverse a list is with something called a script object. Here's is a link to what they are.
http://developer.apple.com/documentatio … jects.html

I can't explain exactly why they're faster but they are tremendously faster when working with large lists. The idea is to put your large list into a property of a script object, and then use that object to work with your list. You'll be amazed at the speed gain. Give it a try with your large list and see how much faster it is! Here's an example of what to do.

Applescript:

set mylist to {"able", "baker", "charlie", "delta", "baker", "able"}
set itemToRemove to "baker"

-- define the script object
script s
   property scriptList : null
   property finalList : {}
end script

-- set your large list to a property in the script object
set s's scriptList to mylist

-- create the new list in another property of the script object
repeat with i from 1 to count of s's scriptList
   if item i of s's scriptList is not equal to itemToRemove then set end of s's finalList to item i of s's scriptList
end repeat
return s's finalList

Offline

 

#6 2009-06-24 08:45:11 am

mikerickson
Member
Registered: 2008-10-26
Posts: 437

Re: deleting items from a list in a repeat

mleonti wrote:

Thanks mikerickson,

I tested and I am sorry to report that with 29456 files in the list it still takes too much time.


I don't have a file to test that extensivly, was my posted script faster than the looping script?

Regulus, thanks for the link to script objects, it looks powerful.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)