gpt4 book ai didi

Applescript 处理程序,重复 i 从 1 到 this_list 的编号

转载 作者:行者123 更新时间:2023-12-03 06:14:25 25 4
gpt4 key购买 nike

如果有人能告诉我我在这里做错了什么,或者为我指明正确的方向以找到答案,我将不胜感激。

我有一个数字列表和相应的名称列表

numList {"1", "2", "4", "6"}
nameList {"bob", "joel", "mickey", "tara", "jason", "stacey"}

我正在尝试创建一个处理程序来从 numList 中获取数字并获取相应的名称作为列表。

The result should be {"bob", "joel", "tara", "stacey"

这就是我所拥有的,但它没有返回正确的名称。

on myFirstHandler(this_list, other_list)
set rest_list to {}
set availlist to {}
repeat with h from 1 to the number of this_list
set the rest_list to item h of other_list
set the end of the availlist to rest_list
end repeat
return availlist
end myFirstHandler

set numList {"1", "2", "4", "6"}
set nameList {"bob", "joel", "mickey", "tara", "jason", "stacey"}

myFirstHandler(numList, nameList)
set AlcoholAvailable to the result

returns {"bob", "joel", "mickey", "tara"}

最佳答案

您的尝试非常非常接近。这是它的修改、更正版本:

on myFirstHandler(this_list, other_list)
set rest_list to {}

repeat with h in this_list
set the end of rest_list to item h of other_list
end repeat

return rest_list
end myFirstHandler


set numList to {1, 2, 4, 6}
set nameList to {"bob", "joel", "mickey", "tara", "jason", "stacey"}

myFirstHandler(numList, nameList)
set AlcoholAvailable to the result

主要的区别在于repeat循环的定义方式:在您最初的尝试中,您循环遍历索引源列表,因此您总是会使用运行 1, 2, 3, 4, 5, 6 的数字,因此从目标列表中提取这些项目。在我的修改版本中,我循环遍历,因此我能够使用源列表中的数字1, 2, 4 , 6.

所以比较一下你的repeat子句:

repeat with h from 1 to the number of this_list

与我的:

repeat with h in this_list

两者都是 AppleScript 中完全有效的位,但执行的操作略有不同:一个使用索引,另一个使用

<小时/>

稍微更先进但更通用的方法如下:

set numList to {1, 2, 4, 6}
set nameList to {"bob", "joel", "mickey", "tara", "jason", "stacey"}

on map(L, function)
local L, function

tell result to repeat with x in L
set x's contents to function's fn(x)
end repeat

L
end map

on itemInList(L)
script
on fn(x)
item x of L
end fn
end script
end itemInList

map(numList, itemInList(nameList))

在这里,我定义了一个“基本”映射函数。 mapping 函数采用一个函数(在本例中为从目标列表返回一个元素的函数)并将该函数应用于数组中的每个元素(在本例中为源列表, numList。这个列表在此过程中被修改,因此您会发现 numList 的元素已被替换为 {"bob", "joel", "tara", "stacey"}.

此构造的特殊之处在于,您可以替换 map 在本示例中使用的函数(处理程序)itemInList() 并定义具有类似性质的其他处理程序对您的列表执行其他操作。

关于Applescript 处理程序,重复 i 从 1 到 this_list 的编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com