1

Say I have:

set U = { "a", "b", "c" }
set V = { 1, 2, 3 }

How can I get "a1", "b2" and "c3"? (V is numbers not strings)

4 Answers 4

3

Does that the trick? :

set U to {"a", "b", "c"}
set V to {1, 2, 3}
set X to {}
repeat with i from 1 to number of items in U
    set end of X to ((item i of U) & (item i of V)) as text
end repeat
Sign up to request clarification or add additional context in comments.

Comments

2

Almost the same code as Zero but it checks for the length of list U and V if one would be shorter than the other. The second difference is getting rid of the parentheses. When you join data in AppleScript and the first item of the join is a string then all the other values are automatically coerced into string objects.

set U to {"a", "b", "c"}
set V to {1, 2, 3}
set i to 1
set R to {}
repeat until i > (count of U) or i > (count of V)
    set end of R to item i of U & item i of V
    set i to i + 1
end repeat
return R

Comments

0

This scripts starts from your example, loops through the first array and adds the items of the second array using a counter. It then converts the new string to a list.

-- your example
set u to {"a", "b", "c"}
set v to {1, 2, 3}
-- prepare variables
set x to "" as text
set counter to 0
-- loop through the two arrays
repeat with c in u as text
    set counter to counter + 1
    set x to x & (c & item counter of v) & ","
end repeat
-- remove last comma
set len to the length of x
set x to characters 1 thru (len - 1) of x as text
-- convert string x to list x
set AppleScript's text item delimiters to ","
set x to every text item of x
-- display result
return x

This script produces {"a1","b2","c3"}

3 Comments

Looks good. As a matter of habit, I always set AppleScript's text item delimiters back to "" when I'm done with them so it doesn't cause unexpected behavior elsewhere in my scripts, or in other scripts also open at the same time in AppleScript Editor.
@IvanX: You have to be careful with that when using nested handlers. It's safer to store the current text item delimiters into a variable, do your thing and restore the text item delimiter back to it's old value rather than setting it back to "".
I hear you, and agree with you, though it rarely applies to my own code. I always write things so that the text item delimiters are never not "" for any longer than absolutely necessary, and don't allow situations where the code can go elsewhere without first getting set back to "". But, yes, your point is correct.
-1

Here is another approach...

set U to {"a", "b", "c"}
set V to {1, 2, 3}
set text item delimiters to ","
set {U, V} to {U as text, V as text}
set text item delimiters to {return & space, return}
set X to text items 1 thru -2 of (do shell script "echo {" & U & "}{" & V & "}'\\n'")
set text item delimiters to ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.