Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Array count for a bidimensional array..

Author
Message
Alduce
23
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 15th May 2010 23:53
Hi again. Someone can tell me if i can obtain the array count of both first and second dimension of an array?

Example :


In my example which of the two dimension return to me the count??
The first? and if yes, how can i obtain the count of the second dimension?

Thanx
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 16th May 2010 06:31
In short... Not really.

Try out this code:

It prints out "120", right? Why the weird result, you may ask? Well... first, one needs to recognize that when you call this:

dim b(10)

that creates an array of 11 elements (one element at 0, all the way through and including 10). When you call this:

dim b(10)
print array count(b)

that prints out "10", even though it has 11 elements, because that's how numbering conventions go in DBPro. So, when you say this:
dim b(10,10)
that creates an array of 11 by 11 elements, 11*11=121, and since zero is *kind of* excluded, you subtract one, giving you 121.

In java, you would say something like this:
Integer[][] a = new Integer[20][12]();
which would literally create 20 arrays of 12 integers. That's not true in DBPro, an array declared by this:
dim arr(a,b,c,d)
would return an array count of ((a+1)*(b+1)*(c+1)*(d+1)-1)

conversely, you could call this:

dim arr(10,10)
for n=0 to 120
arr(n)=rnd(100)
next n
print arr(5,5)

...weird stuff going on there.

So, that's what I mean when I say basically: No. You're much better off storing it as a separate variable whenever you declare or resize the array. You can get the total size of the array, compressed into one dimension, but there's no way I know of to get the size of each dimension separately.


Alduce
23
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 16th May 2010 12:14
Thank you very much Neuro Fuzzy, your answer it's really complete!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th May 2010 13:51
If you have my plug-in set then you have the function you need already - I added an extension to the ARRAY COUNT function to do this: ARRAY COUNT

The 'Dimension' parameter starts at 1.

Alduce
23
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 16th May 2010 13:54
Ahh perfect! This was exactly what I wanted!

Thank you

Login to post a reply

Server time is: 2026-07-25 23:12:18
Your offset time is: 2026-07-25 23:12:18