www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Length of Dimension in Multidimensional Array?

reply "Ralf the Bat" <email email.email> writes:
Probably a stupid question, but how do I find the length of each
dimension in a multidimensional array?
Jun 05 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
Jun 05 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/5/15 3:53 AM, Ralf the Bat wrote:
 Probably a stupid question, but how do I find the length of each
 dimension in a multidimensional array?
if you mean a static array, you can get each dimension using .length: int[2][3] arr; static assert(arr.length == 3); static assert(arr[0].length == 2); There is likely a way you can get a tuple of the lengths at compile time. If you mean a dynamic multidim array, each array element can have its own independent set of dimensions. -Steve
Jun 05 2015