gpt4 book ai didi

julia - 如何打破 Julia 中的嵌套 for 循环

转载 作者:行者123 更新时间:2023-12-04 00:12:35 39 4
gpt4 key购买 nike

我试图以一种非常无效的方式打破嵌套循环:

BreakingPoint = false
a=["R1","R2","R3"]
b=["R2","R3","R4"]
for i in a
for j in b
if i == j
BreakingPoint = true
println("i = $i, j = $j.")
end
if BreakingPoint == true; break; end
end
if BreakingPoint == true; break; end
end

有没有更简单的方法来做到这一点?在我的实际问题中,我不知道数组中的内容 ab , 除了他们是 ASCIIString s。数组名称(示例代码中的 ab)也是通过元编程方法自动生成的。

最佳答案

你可以做两件事之一

在多外循环中有循环语句(如果那是它的名字)

for i in a, j in b
if i == j
break
end
end

这是干净的,但并不总是可能的

我会因为提出这个建议而被钉在十字架上,但你可以使用@goto 和@label
for i in a
for j in b
if i == j
@goto escape_label
end
end
end

@label escape_label

如果您使用@goto/@label 方式,为了维护/审查代码的人,请正确记录您的使用,因为使用标签导航代码非常烦人

关于多环中断的讨论,见 this

关于julia - 如何打破 Julia 中的嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796234/

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