How do you iterate "vertically" over a 2-D array?
Chris Katko
ckatko at gmail.com
Tue Oct 9 10:52:47 UTC 2018
I have a 2-D array:
int[5][5] data =
[
[1, 0, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 1, 0],
[1, 1, 1, 1, 0]
];
1 - Is there a way to foreach vertically through that? (that is,
rotated 90 degrees from the usual iteration.)
2 - Is there a way to send that "vertical slice" to a function?
int count_numbers(){/*...*/}
count_numbers(data[0]); //horizontal sum of array[0][0..$-1]
WORKS.
count_numbers(data[][0]); //vertical sum of array[0..$-1][0]
HYPOTHETICAL
I'm working on an RLE encoding function and I need both vertical
and horizontal strips. It just occurred to me that the problem is
essentially the same, if you can "transpose" the matrix.
Oh geez, did I just realize what I need? some sort of matrix
transpose function? I found this post;
https://forum.dlang.org/post/na8ikk$2ojo$1@digitalmars.com
The problem is though, even if I use [][], instead of [5][5] (or
try using his conversion function), I can call transposed... but
I can't call my function with it!
void run_rle3(int [] a){/*...*/}
run_rle3(data[0]); //works
run_rle3(data.transposed[0]); //nope [see error below]
run_rle3(data[0].transposed); //nope (didn't expect this to)
run_rle3((data.transposed)[0]); //nope [see error below]
Error: function dmap.run_rle3 (int[] a) is not callable using
argument types (Transversal!(int[][], cast(TransverseOptions)0))
More information about the Digitalmars-d-learn
mailing list