May 2019
Beginner to intermediate
466 pages
10h 44m
English
The simplest way to iterate over an array is with the for construct:
for element in yourarray
# do something with element
end
Here's an example:
julia> for person in ["Alison", "James", "Cohen"]
println("Hello $person")
end
Hello Alison
Hello James
Hello Cohen
If you also need the index while iterating, Julia exposes the eachindex(yourarray) iterator:
julia> people = ["Alison", "James", "Cohen"]
3-element Array{String,1}:
"Alison"
"James"
"Cohen"
julia> for i in eachindex(people)
println("$i. $(people[i])")
end
1. Alison
2. James
3. Cohen
Read now
Unlock full access