how do I convert…
SomeList1 = {“10,3,5,1”}
and/or
SomeList2 = {“10”,“3”,“5”,“1”}
to
mySomeList3 = {10,3,5,1}
how do I convert…
SomeList1 = {“10,3,5,1”}
and/or
SomeList2 = {“10”,“3”,“5”,“1”}
to
mySomeList3 = {10,3,5,1}
here’s one possible solution:
property SomeList2 : {“10”, “3”, “5”, “1”}
set i to 0
set someList_Temp to {}
repeat with i from 1 to length of SomeList2
set end of someList_Temp to (get item i of SomeList2 as integer)
end repeat
set SomeList2 to someList_Temp
–this should return someList2 as {10, 3, 5, 1}
good luck!
set SomeList1 to {"10,3,5,1"}
set mySomeList3 to {}
set mySomeList1 to (SomeList1 as string)
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with tempItem in (text items in mySomeList1)
set mySomeList3 to mySomeList3 & (tempItem as number)
end repeat
set AppleScript's text item delimiters to delim
return mySomeList3
– OR –
set SomeList1 to {"10", "3", "5", "1"}
set mySomeList3 to {}
repeat with tempItem in (text items in SomeList1)
set mySomeList3 to mySomeList3 & (tempItem as number)
end repeat
return mySomeList3
j
ken
you should make the last code as:
set SomeList3 to someList_Temp
that’s if you want the list to be labeled as SomeList3
Hi Ken,
use jobu’s solution…best and straight to the point. also less codes.
i never cease to learn from jobu…many a times, he has bailed me out of my scripting problems. thanks, j.
archseed
No need to use a temp list (unless, of course you need to keep the original):
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
This version will work for both SomeList1 and SomeList2:
set SomeList1 to {"10,3,5,1"} -- {"10", "3", "5", "1"}
set mySomeList3 to {}
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
-- When AppleScript's text item delimiters are set to ",", coercing a list to
-- string inserts "," between each item in the list. So both the above lists
-- would be coerced to "10,3,5,1". Getting the 'text items' of this string will
-- break it up again at the commas --> {"10", "3", "5", "1"}.
repeat with tempItem in (text items of (SomeList1 as string))
set end of mySomeList3 to (tempItem as number)
end repeat
set AppleScript's text item delimiters to delim
return mySomeList3
One possible limitation with the solutions offered so far is that there must be no spaces in the source strings. The following allows that, though it may be a trifle slow:
set SomeList1 to {"10, 3, 5, 1"} -- {"10", "3", "5", "1"}
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
-- Concatenating a list to a string automatically coerces the list to string
-- using the current value of the text item delimiters.
set mySomeList3 to (run script ("{" & SomeList1 & "}"))
set AppleScript's text item delimiters to delim
return mySomeList3
Thanks for all the tips. I just wan’t sure if I was going to need a repeat or if there was just n easier way to turn the list into numbers. The string is being placed by the user in a text edit box. Is this input always a string?
I decided to make it into a sub-routine as I thought I might be needing it in a couple of places…
set myCoercedList to my coerced(myUserList)
--
on coerced(x)
set myCoercedList to {}
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with i in (text items of (x as string))
set end of myCoercedList to (i as number)
end repeat
set AppleScript's text item delimiters to oldDelimiters
return myCoercedList
end coerced
--
now I can sort it…
set mysortlist to my sort(myCoercedList)
--
on sort(l)
if length of l = 1 then return l
set x to item 1 of l
set ll to rest of l
set l1 to {}
set l2 to {}
repeat with i in ll
if x < i then
set l2 to l2 & i
else
set l1 to l1 & i
end if
end repeat
if length of l1 > 1 then set l1 to sort(l1)
if length of l2 > 1 then set l2 to sort(l2)
return l1 & x & l2
-- If you want a descending order,
-- use "if x>i" instead of "if x<i" in the eighth line.
-- This sorting routine works on strings as well as on numbers.
end sort
--
Thanks Again!!
ok ive Coerced and Sorted…now trying to match…I can get this to work except for returning the variable myBadPageList from the sub-routine. Is it possible t return two values. As it is right now myGoodPageList is returning… See Code
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
I’m not entirely certain what you’re trying to do but certainly your subroutine won’t work right since you’ve got the same variable (i) iterating through two lists at the same time. I think this is what you want (or close to it):
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
jonn8
thanks for the fix, I’m not exactly sure what I am trying to do either…I forgot to add the delimiter portion of the code in my last post sorry :oops: So I was actually just returning a comma delim srting of numbers.
I was getting the matching items to show up but couldn’t figure out how to get both the matching and non matching items to be returned.
I’ll have to sit down and take a look at your code and try to make some sense out of it but I looks like it will do what I need to have done.
I am curious about the line…
tell (a reference to my text item delimiters)
set {o, contents} to {contents, ", "}
What is this doing? Its the first time I’ve come across it in use. I usually do the standard set to oldDelim change to new delim then set back to old delim.
until next time.