James Beezho
James Beezho

Reputation: 491

Julia Find Index of Iterative Value

Im trying to find the index of an item in an array within a for loop that steps from item to item in the Array. Is there a built in function that allows me to do this?

dim = 3
length = 10

arrayTuple = fill!(Array(Int64, dim),length)
# [10,10,10]

Arr = fill!(Array(Int64,tuple(arrayTuple...)),1)

for item in Arr
    #print the index of node into array here
end

Upvotes: 4

Views: 2151

Answers (1)

DSM
DSM

Reputation: 353579

IIUC, you can use enumerate:

julia> for (i, item) in enumerate(Arr[1:5])
           println(i, " ", item)
       end
1 1
2 1
3 1
4 1
5 1

If you want the multidimensional version, you could use eachindex instead:

julia> for i in eachindex(a)
           println(i, " ", a[i])
       end
Base.IteratorsMD.CartesianIndex_3(1,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(2,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(3,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(1,2,1) 1.0
[... and so on]

Upvotes: 10

Related Questions