Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do you determine the length of an array?
#1
lets say i have an array names(i) that has a total of 8 records..
names(0) thru names(7). is there a command i can use to determine how many records ar stored in that array?

something like ArrayLen = Length(Names).
i know the work around already, but i was hoping theres a simple command for this that ive been overlooking.

thanx in advance
url]http://qb45.think-new.com[/url]
Reply
#2
Something like LBOUND and UBOUND?

http://qbasicnews.com/qboho/qcklbound.shtml
img]http://usuarios.vtr.net/~disaster/sigs/annoyizer.php[/img]
Reply
#3
Try this:
Code:
ArrayLen = UBOUND(Names, 1) - LBOUND(Names, 1) + 1

The way this works is like this: UBOUND(Names, 1) returns the upper bound (or highest index if you like) of the first dimension of the array Names, and LBOUND(Names, 1) returns the lowest index. Thus for your example, UBOUND(Names, 1) returns 7 and LBOUND(Names, 1) returns 0. So now all you do is take the LBOUND from the UBOUND. But here's the trick: you must add one to the result, because the first index is counted too. eg Names 0,1,2,3,4,5,6,7 is 8 records, so UBOUND - LBOUND = 7 - 0 = 7, then you must add 1 for the 0 record.

You may emit the ", 1" when using UBOUND and LBOUND eg ArrayUpper = UBOUND(Names). If you want the upper bound of another dimension just add the number of the dimension.

Hope that helps...
Reply
#4
awesome, thank guys, i knew there was a command for it... but couldnt find it.
url]http://qb45.think-new.com[/url]
Reply
#5
Cool Smile

I'm gonna put that in the wiki. It's a newbie question and thus should be added. Is anyone else actually doing this, like the wiki was supposed to be?
Reply
#6
Remember that not everybody has access to edit the WIKI.

BTW, na_th_an *actually* cares about that. Good guy, Nate. Tongue
img]http://usuarios.vtr.net/~disaster/sigs/annoyizer.php[/img]
Reply
#7
Everyone can view it though, right?

What I meant was that when a newbie question is answered someone with access should post something in the wiki. If all with access just look around and do this then soon the wiki will become one of the most useful QBasic FAQs on the web. I have asked wildcard whether I can link to it when my site is up, but at the moment it isn't very full, so let's get adding and get the word out.

Quote:BTW, na_th_an *actually* cares about that. Good guy, Nate. Tongue

Amen.
Reply
#8
why do you need to know the length if you already defined it? Isn't that just repetitive?
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#9
Quote:why do you need to know the length if you already defined it? Isn't that just repetitive?

Well, yeah, but some people define it with the constant right in the DIM. I'd do it like this, personally:

CONST numsprites = 20

DIM mysprites(1 TO numsprites) AS ...
Reply
#10
Having the subroutine be able to determine the array's properties makes the subroutine more self-contained and possibly more adaptive to being used generically.
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)