Simple loop to use previous loop value

I have issue to run this supposedly simple loop. Tested two methods, but both spit errors “ERROR: LoadError: UndefVarError: ii not defined”. My objective to use the “i” from previous loop and current loop.

#method 1
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

Thanks.

This can be sometimes unintuitive behavior when working in the REPL. Put global ii at the top of your for loop. For more info see the docs.

1 Like

Did that, but still errors. I ran this in Atom/Juno

#Julia ver. 1.4
global ii
#method 1
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

Sorry if I wasn’t clear,

you want this:

#Julia ver. 1.4
#method 1
for i in range(0.0, length=6, step=0.1)
    global ii
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    global ii
    if i > 0
        println(ii)
    end
    ii = i
end

You can also wrap it in a let block.

let ii = 0
    for i in range...
        ...
    end
end
3 Likes

Thank you. It is working.

I also found alternative solution from the link below. Adapted for my problem above.
https://stackoverflow.com/questions/48345168/julia-previous-and-next-values-inside-a-for-loop

List1 = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]

for i = 2:length(List1)
	println("The previous letter of $(List1[i]) is $(List1[i-1])")
end

OR

x = collect(0.0:0.1:0.5)
@show x
for i in 2:length(x)
    println(x[i])
    println(x[i-1])
    ii = i
end

Check out the package ShiftedArrays for more easy lags and leads.

You are hurting my head. In the original question you appear to want to calculate a value in one loop and use that value in another. In the stack overflow solution you want to get the previous value in a list.

These are two different actions, and have two different solutions…It might make sense in the future to state what you are trying to do at a high level, rather than asking for some low level solution. You should get an answer that solves the correct problem.

1 Like

Sorry if I caused confusion. My objective has not changed, i.e. to get current value and previous loop value and doing something with the data. The example from stackoverflow.com is pretty much shows what I desired. My original question can be expanded to have the loop shows both current “i” value and previous “i” value. The list can be made from range() function or anything similar e.g. collect() function.

Thanks @pdeffebach for the pointer. Not sure how this can help. It may make my code more complicated. I just want to loop over the range() for data binning, i.e. for lower bound and upper bound.

Note that x is completely allocated in memory in this solution (in contrast to range, which is an iterator), therefore you should not use this approach for a range with many elements.