Length of Dimension in Multidimensional Array?
Dennis Ritchie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 5 02:31:30 PDT 2015
On Friday, 5 June 2015 at 07:53:52 UTC, Ralf the Bat wrote:
> Probably a stupid question, but how do I find the length of each
> dimension in a multidimensional array?
I slightly modified the function deepDup, who came up Ali Çehreli
:)
http://beta.forum.dlang.org/post/mihl6m$1che$1@digitalmars.com
import std.stdio, std.traits, std.range, std.algorithm;
auto deepLen(A)(A arr)
if (isArray!A)
{
writefln("%s.len = %s", arr, arr.length);
static if (isArray!(ElementType!A)) {
return arr.map!(a => a.deepLen).array;
} else {
return arr;
}
}
void main() {
auto arr = [[[[[1, 2, 3, 4], [0]], [[4, 3], [5, 6, 7]]]]];
arr.deepLen;
}
[[[[[1, 2, 3, 4], [0]], [[4, 3], [5, 6, 7]]]]].len = 1
[[[[1, 2, 3, 4], [0]], [[4, 3], [5, 6, 7]]]].len = 1
[[[1, 2, 3, 4], [0]], [[4, 3], [5, 6, 7]]].len = 2
[[1, 2, 3, 4], [0]].len = 2
[1, 2, 3, 4].len = 4
[0].len = 1
[[4, 3], [5, 6, 7]].len = 2
[4, 3].len = 2
[5, 6, 7].len = 3
More information about the Digitalmars-d-learn
mailing list