Applescript处理程序,使用从1到该列表编号的i重复此操作
如果有人能告诉我我做错了什么,或者给我指出正确的方向来找到答案,我将不胜感激 我有一个数字列表和一个对应的名字列表Applescript处理程序,使用从1到该列表编号的i重复此操作,applescript,handler,Applescript,Handler,如果有人能告诉我我做错了什么,或者给我指出正确的方向来找到答案,我将不胜感激 我有一个数字列表和一个对应的名字列表 numList {"1", "2", "4", "6"} nameList {"bob", "joel", "mickey", "tara", "jason", "stacey"} 我正在尝试创建一个处理程序,从numList获取数字,并将相应的名称作为列表 The result should be {"bob", "joel", "tara", "stacey" 这是我所拥有
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
主要的关键区别在于重复
循环的定义方式:在最初的尝试中,您循环了源列表的索引,因此您总是要使用运行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))
在这里,我定义了一个“基本”映射函数。映射
函数在本例中接受一个函数,该函数从目标列表返回一个元素,并将该函数应用于数组中的每个元素。在本例中,是源列表,numList
。这个列表在这个过程中被修改,因此您会发现numList
的元素已经被{“bob”、“joel”、“tara”、“stacey”}
替换
这个构造的特殊之处在于,您可以替换本例中map
使用的函数(处理程序),itemInList()
,并定义其他类似性质的处理程序,对列表执行其他操作。是的,使用值的不同正是我在这里需要的。再次感谢你,非常感谢!我需要赞扬你写了一个很好的问题:你陈述了你的问题,你期望的结果是什么,你得到了什么结果,以及你用来得到这个结果的代码。做得好。它使帮助变得更容易和更愉快+谢谢你。哦,天哪,谢谢你的赞美和帮助。