Re: Find index of list to find item in another list
Re: Find index of list to find item in another list
- Subject: Re: Find index of list to find item in another list
- From: Deivy Petrescu <email@hidden>
- Date: Wed, 1 Jul 2009 15:56:43 -0400
On 01/07/2009, at 14:20 , Jim Skibbie wrote:
I ended up converting the list to a comma separated string then
using a ruby shell script to access Ruby’s .index method on the array:
--the really big lists
set X to {"123456", "123457", "123458"}
set Y to {{"A", "B", "C"}, {"D"}, {"E", "F"}}
-- the item I'm looking for
set Z to "123457"
set text item delimiters to ","
set X to X as string
set text item delimiters to ""
set Indexcmd to "ruby " & quoted form of "(path to shell script)/
IndexInArray.rb" & " " & quoted form of X & " " & quoted form of Z
set IndexNumber to do shell script Indexcmd
if IndexNumber is not "" then set FoundThis to item ((IndexNumber as
integer) + 1) of Y
where the shell script is a ruby script that takes in the long
string, turns it into an array and then uses the .index method to
compare if “Z” is in “X” and where it is index wise. I don’t know
enough about command line ruby to know if it’s possible to wrap this
all into a “do shell script” command that would not have to read in
my external ruby shell script.
After all this, I’m not sure if this is faster or not. The incoming
data comes from a web service, so it’s hard to judge if the whole
script runs any faster iterating through the list or using the ruby
shell.
Shell script for file: IndexInArray.rb
#!/usr/bin/ruby
incoming_string = ARGV[0]
does_array_contain_this = ARGV[1]
converted_array = incoming_string.split(",")
puts converted_array.index(does_array_contain_this)
Thanks.
Jim
Well Jim,
You could have used just plain AppleScript without going through so
many hoops.
set X to {"123456", "123457", "123458"}
set Y to {{"A", "B", "C"}, {"D"}, {"E", "F"}}
-- the item I'm looking for
set Z to "123457"
if X contains Z then set theindex to getitemindex(X, Z)
---->2
on getitemindex(thelist, theitem)
set AppleScript's text item delimiters to return
set thelist to thelist as text
return count of paragraphs in (text 1 thru (get offset of theitem in
thelist) of thelist)
end getitemindex
Deivy Petrescu
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden