# Thread: Find number of rows and columns in a multidimensional array

1. ## Find number of rows and columns in a multidimensional array

How to find the number of rows and no of rows in a multidimensional array?

2. ## Re: Find number of rows and columns in a multidimensional array

I don't think this is a particularly easy question to answer, particularly due to the fact that WR does not really implement multi-dimensional arrays: it really stores them as 1-dim arrays. And how do you handle arrays with more than 2 dimensions? In any case, note that mutlidimensional indexes are stored as strings with a special character separating the subscripts, "\034". So if you loop through the indices of an array with a "for(ix in array)" expression, you could then do "split(ix, subs, "\034");" to break each subscript into its component pieces (in array subs[] in this example).

But perhaps you could tell us why you need to know this information and then we could suggest a solution to the larger problem?

3. ## Re: Find number of rows and columns in a multidimensional array

Maybe you need something like this function:

</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">static multArr[] =
{ {1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12},
{13, 14, 15, 16},
{17, 18, 19, 20} };

static cntArr[], cntArr1[];

function arr_count (inout array[], out array_count[])
{
auto i, j, nrDimensions, hlpArr[], parentItem, nrParentItems;

nrParentItems = 0;
for (i in array)
{
nrDimensions = split(i, hlpArr, &quot;\034&quot;
parentItem = &quot;&quot;;
for (j=1; j &lt; nrDimensions; j++)
parentItem = parentItem &amp; &quot;\034&quot; &amp; hlpArr[j];
parentItem = substr(parentItem, 2); # remove leading &quot;\034&quot;

if (array_count[parentItem] == &quot;&quot
{
array_count[parentItem] = 1;
nrParentItems++;
}
else
array_count[parentItem]++;
}
return nrParentItems;
}

# -------------------------

rc = arr_count(multArr, cntArr); # first aggregation
rc1 = arr_count (cntArr, cntArr1); # second aggregation</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">This function aggregates the array indices and stores their number (count) into a new array. Return code is the number of items in the aggregation array.
In this example the first call of this function returns an array with 5 items:
cntArr[0] = 4, cntArr[1] = 3, cntArr[2] = 5, cntArr[3] = 4, cntArr[4] = 4.

You can continue calling this function until it returns 1. This means that you are at the top level and the function returns an array with 1 item:
cntArr[""] = 5 (5 rows are in this multidimensional array)

Regards,
Juergen

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.