If I have an array like this:
using OffsetArrays
a = OffsetArray(collect(1:5),(11:15))
I can iterate through the array with:
for (i,x) in enumerate(a)
println((i,x))
end
and get:
(1, 1)
(2, 2)
(3, 3)
(4, 4)
(5, 5)
But I want this:
(11, 1)
(12, 2)
(13, 3)
(14, 4)
(15, 5)
Is there a way to get the real index since I am using an offset array?
While the OFFSET function moves from the starting point by a certain number of rows and/or columns, INDEX finds a cell at the intersection of a particular row and column.
To get the first index of an item in an array in Swift, use the array. firstIndex(where:) method. print(i1!)
The index indicates the position of the element within the array (starting from 1) and is either a number or a field containing a number.
The function pairs
respects the indexing behaviour:
julia> using OffsetArrays
julia> a = OffsetArray(collect(1:5),(11:15))
julia> for (i,x) in pairs(a)
println((i,x))
end
(11, 1)
(12, 2)
(13, 3)
(14, 4)
(15, 5)
from the docs:
Base.pairs
— Function.
pairs(collection)
Return an iterator over
key => value
pairs for any collection that maps a set of keys to a set of values. This includes arrays, where the keys are the array indices.pairs(IndexLinear(), A) pairs(IndexCartesian(), A) pairs(IndexStyle(A), A)
An iterator that accesses each element of the array
A
, returningi => x
, wherei
is the index for the element andx = A[i]
. Identical topairs(A)
, except that the style of index can be selected. Also similar toenumerate(A)
, excepti
will be a valid index forA
, whileenumerate
always counts from 1 regardless of the indices ofA
.Specifying
IndexLinear()
ensures thati
will be an integer; specifyingIndexCartesian()
ensures thati
will be aCartesianIndex
; specifyingIndexStyle(A)
chooses whichever has been defined as the native indexing style for arrayA
.Mutation of the bounds of the underlying array will invalidate this iterator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With