Hi,
I apologize if this is a very elementary mistake but I still struggling with programming and Julia. I would like to iterate through a list while a certain conditions hold true. I would also like to be able to stop the process with an outside code at any time without resorting to the ctrl c method. I thought the most intuitive way to handle this task would be to nest a for loop in a while loop.
With a while loop I can do something like this
function sqrt(x)
repeat = Ref(true)
count = 0
@async while count< 5 && repeat[]
println(" the square root of $x is $(x^(1/2)) ")
count+=1
sleep(5)
end
return(repeat)
end
If I input a = sqrt(4) I would get something like the following
julia> b=sqrt(4)
the square root of 4 is 2.0
Base.RefValue{Bool}(true)
julia> the square root of 4 is 2.0
julia> the square root of 4 is 2.0
Because of the “count” condition this would continue for 5 times or I could terminate the program at anytime by entering
julia> b[]=false
false
However once I turn x into a list and try to iterate through it with a for loop, (nest a for loop in a while loop) it appears that the while loop conditions no longer are being tested.
e.g.
function sqrt(sets)
repeat = Ref(true)
count = 0
@async while count< 5 && repeat[]
for i in sets
try
println("the square root of $i is $(i^(1/2))")
count+=1
sleep(5)
catch
continue
end
end
end
return(repeat)
end
I get something like this.
julia> a = sqrt([4,16,25,36,81,100])
the square root of 4 is 2.0
Base.RefValue{Bool}(true)
julia> the square root of 16 is 4.0
julia> the square root of 25 is 5.0
julia> the square root of 36 is 6.0
julia> a[]=false
false
julia> the square root of 81 is 9.0
the square root of 100 is 10.0
As you can see the “count” condition no longer limits the lines of output and I am no longer able to terminate the program with the reference call. Consequently I assume that the conditions of the while loop are being ignored because of the way I nested the for loop. If anyone has any insights on what is happening and the proper way to approach something like this in Julia it would be greatly appreciated. Thank you.