Difference handling missing of enumerate and eachindex

They are two different things: eachindex gives you (valid) indices that you can index with, enumerate just counts from 1 for any arbitrary iterator.

Here is another, perhaps more illustrative, example:

julia> using OffsetArrays

julia> v = OffsetVector([1, 2, 3], -1);

julia> for (i, _) in enumerate(v)
           @show i
       end
i = 1
i = 2
i = 3

julia> for i in eachindex(v)
           @show i
       end
i = 0
i = 1
i = 2

(It is preffered to just copy and paste your code instead of including a screenshot – images are not searchable, and it is not possible for people that want to help to copy the code and play around. In this case you did both, but the image doesn’t really add anything.)

3 Likes