AppleScriptに単純な「repeatwith」があり、条件付きで「repeat」の次の項目に進みたいと思います。基本的に私は他の言語で「続ける」(または中断する?)に似たものを探しています。
私はAppleScriptに精通していませんが、今では数回便利だと思っています。
AppleScriptに単純な「repeatwith」があり、条件付きで「repeat」の次の項目に進みたいと思います。基本的に私は他の言語で「続ける」(または中断する?)に似たものを探しています。
私はAppleScriptに精通していませんが、今では数回便利だと思っています。
この正確な問題を検索した後、私はこの本の抜粋をオンラインで見つけました。repeat
これは、現在の反復をスキップして、ループの次の反復に直接ジャンプする方法の質問に正確に答えます。
Applescriptにはexit repeat
、ループを完全に終了し、残りのすべての反復をスキップするがあります。これは無限ループで役立つ場合がありますが、この場合は必要ありません。
どうやらcontinue
AppleScriptには同様の機能は存在しませんが、それをシミュレートするための秘訣は次のとおりです。
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- # actual loop
repeat 1 times -- # fake loop
set value to item 1 of anItem
if value = "3" then exit repeat -- # simulated `continue`
display dialog value
end repeat
end repeat
これにより、1、2、4、および5のダイアログが表示されます。
ここでは、2つのループを作成しました。外側のループは実際のループであり、内側のループは1回だけ繰り返されるループです。は内側のexit repeat
ループを終了し、外側のループを続行します。まさに私たちが望むものです。
明らかに、これを使用すると、通常のことを行う能力が失われますexit repeat
。
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- # actual loop
try
set value to item 1 of anItem
if value = "3" then error 0 -- # simulated `continue`
log value
end try
end repeat
これはまだあなたに「出口リピート」の可能性を与えます
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- # actual loop
try -- # needed to simulate continue
set value to item 1 of anItem
if value = "3" then continueRepeat -- # simulated `continue` throws an error to exit the try block
log value
on error e
if e does not contain "continueRepeat" then error e -- # Keeps error throwing intact
end try
end repeat
上記のtryブロックベースのアプローチに基づいていますが、読み取りが少し良くなります。もちろん、continueRepeatが定義されていないため、エラーがスローされ、残りのtryブロックがスキップされます。
エラースローをそのまま維持するには、予期しないエラーをスローするonerror句を含めます。
-または、別の戦略を使用することもできます。ループを使用してループし、次のようにハンドラーで条件付きロジックを実行します。
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList
doConditionalWork(anItem as string)
end repeat
on doConditionalWork(value)
if value = "3" then return
display dialog value
end doConditionalWork
Y'allはすべてそれを複雑にしすぎています。これを試して:
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList
set value to item 1 of anItem
if value is not "3" then log value
end repeat
条件付きでのみ繰り返されるループには、「repeatwhile」を使用することもできます。
Tom Lokhorstの回答に基づいて、設定した変数に基づいて、break
またはのいずれかを実行するバリアントを次に示します。そのために、外側の(偽物ではない)ループの最後に、continue
isExit
if isExit then
exit repeat
end if
そうすればisExit
、trueの場合、外側のループも終了するだけで、効果的break
です。
元の質問の問題の場合、これは次のようになります。以下は一般化されたものです。
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- actual loop
set isExit to false
repeat 1 times -- fake loop
-- do your stuff
set value to item 1 of anItem
if value = "3" then
-- simulated `continue`
set isExit to false
exit repeat
end if
if value = "69" then
-- simulated `break`
set isExit to true
exit repeat
end if
display dialog value
end repeat
if isExit then
exit repeat
end if
end repeat
したがって、これをより簡単な例に分解するには、次のようにします。
continue
どちらか、またはbreak
これらの2つに入れたいとしましょうif
。これらの間にすべてのコードを配置できます。ここでは、さまざまな出口戦略に関連するコードのみを含めました。
このようなものを書きたい場合:
repeat <condition_repeat>
if <condition_continue> then
continue
end if
if <condition_break> then
break
end if
end repeat
それは次のように書くことができます
repeat <condition_repeat>
set isExit to false -- added
repeat 1 times -- added
if <condition_continue> then
set isExit to false -- changed
exit repeat -- changed
end if
if <condition_break> then
set isExit to true -- changed
exit repeat -- changed
end if
end repeat -- added
if isExit then -- added
exit repeat -- added
end if -- added
end repeat