Loops 在AppleScript中同时循环2个数组

Loops 在AppleScript中同时循环2个数组,loops,applescript,Loops,Applescript,假设我有: set U = { "a", "b", "c" } set V = { 1, 2, 3 } 如何获取“a1”、“b2”和“c3”?(V是数字而不是字符串)此脚本从您的示例开始,循环第一个数组,并使用计数器添加第二个数组的项。然后将新字符串转换为列表 -- 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

假设我有:

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

如何获取“a1”、“b2”和“c3”?(V是数字而不是字符串)

此脚本从您的示例开始,循环第一个数组,并使用计数器添加第二个数组的项。然后将新字符串转换为列表

-- 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

此脚本生成
{“a1”、“b2”、“c3”}

此脚本从您的示例开始,循环第一个数组,并使用计数器添加第二个数组的项。然后将新字符串转换为列表

-- 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
此脚本生成
{“a1”、“b2”、“c3”}

这是技巧吗

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
这就是诀窍吗

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

几乎与Zero相同的代码,但它检查列表U和V的长度,如果其中一个比另一个短。第二个区别是去掉括号。当您在AppleScript中连接数据,并且连接的第一项是字符串时,所有其他值将自动强制转换为字符串对象

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

几乎与Zero相同的代码,但它检查列表U和V的长度,如果其中一个比另一个短。第二个区别是去掉括号。当您在AppleScript中连接数据,并且连接的第一项是字符串时,所有其他值将自动强制转换为字符串对象

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

这是另一种方法

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 ""

这是另一种方法

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 ""

看起来不错。作为一种习惯,我总是在使用完AppleScript的文本项分隔符后将其设置回“”,这样就不会在我的脚本中的其他地方或在AppleScript编辑器中同时打开的其他脚本中导致意外行为。@IvanX:在使用嵌套处理程序时,必须小心这一点。将当前文本项分隔符存储到变量中,执行操作并将文本项分隔符恢复为其旧值比将其设置为“”更安全。我听到了您的意见,并同意您的看法,尽管它很少适用于我自己的代码。我总是写一些东西,这样文本项分隔符在绝对必要的时间内永远不会是“”的,并且不允许代码在没有首先设置回“”的情况下转到其他地方。但是,是的,你的观点是正确的。看起来不错。作为一种习惯,我总是在使用完AppleScript的文本项分隔符后将其设置回“”,这样就不会在我的脚本中的其他地方或在AppleScript编辑器中同时打开的其他脚本中导致意外行为。@IvanX:在使用嵌套处理程序时,必须小心这一点。将当前文本项分隔符存储到变量中,执行操作并将文本项分隔符恢复为其旧值比将其设置为“”更安全。我听到了您的意见,并同意您的看法,尽管它很少适用于我自己的代码。我总是写一些东西,这样文本项分隔符在绝对必要的时间内永远不会是“”的,并且不允许代码在没有首先设置回“”的情况下转到其他地方。但是,是的,你的观点是正确的。