maoam
maoam

Reputation: 321

Matching indices of arrays of different sizes

Let's say we have a DATA array consisting of 23 elements, And INDICATORS array of 3 elements (might be whatever size you want: 3, 5, 7 etc but less then DATA array)

Let's say I'm iterating over DATA array, and starting from, say, index 4 in the DATA array, I need to start highlighting INDICATORS one by one, starting from the first, then second, then third and then wrapping back to the first.

For example:

DATA indexes:0...(4, 5, 6) (7, 8, 9)...22

INDICATORS indexes: (0, 1, 2) (0, 1, 2) ... etc

So basically I need to convert index 4 of DATA array to index 0 of the INDICATORS array, index 5 of DATA array to index 1 of the INDICATORS array etc.

dataArrayIndex % indicatorsArraySize doesn't work in this case.

How do I do that? Thanks.

Upvotes: 0

Views: 48

Answers (1)

Thomas Jager
Thomas Jager

Reputation: 5265

dataArrayIndex % indicatorsArraySize

Won't work for you because you have that starting index.

Instead, you have to subtract your starting index from the dataArrayIndex first:

(dataArrayIndex - dataStartIndex) % indicatorsArraySize

Alternatively, as you iterate, you can compare your current indicatorsArrayIndex to indicatorsArraySize, after incrementing indicatorsArrayIndex. If they're equal, reset indicatorsArrayIndex to 0.

Upvotes: 1

Related Questions