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 Marck Petrescu <email@hidden>
- Date: Fri, 3 Jul 2009 13:07:20 -0400
On Jul 3, 2009, at 12:59 PM, Skeeve wrote:
Deivy Petrescu schrieb:
Well Jim,
You could have used just plain AppleScript without going through so
many hoops.
Well, Deivy…
besides the already mentioned fact that embedded returns don't work,
your solution is plain wrong.
Skeeve,
My solution works!
It did for the presented problem.
The original code is Emmanuel's, of Satimage fame!
His code has the -1 in there, but if you put the -1 in this case, it
will fails.
As with the return code, you can fix it (your and Emmanuel's
solution), but why work hard if the problem at hand is simpler.
The particular solution is there, work for the most general case.
See:
set X to {"123456", "oops123457wrong", "123457", "123458"}
set Y to {{"A", "B", "C"}, {"nope"}, {"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 instead of 3
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
But you can fix it with this:
on getcorrectitemindex(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 return
& theitem & return in thelist) of thelist)) - 1
end getcorrectitemindex
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
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