Qbasicnews.com

Full Version: Counting arrays?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First, apologies if this has been asked before, I looked through a few search results(onqbnews.com, of course) and couldn't find what I was looking for.

Basically, I wanted to get back into QBasic, so I thought i'd write a little text rpg, using different functions to help me learn a bit.

I want to print out the players inventory, and I thought the best way to do this would be to store it in an array, then when the user wants to see it use a sub to display it. But, I have no idea how to count the number of items in an array. Some of this will look bizarre to you, and if you have a better suggestion please do let me know, since of course, i'm still relatively new to this.

So, is there a way to count an array???

Thanks alot,
Jamie :neo:
x
size of array == upper bound - lower bound + 1

if you declare your arrays with a lower bound of 0, then,

size of array == upper bound + 1

Lookup the functions LBOUND and UBOUND.
Couldn't you use something like...

Code:
DIM inventory$(10) 'or whatever

FOR ctr = 1 to 10 'go through array
   IF inventory$(ctr) <> "" Then arrayttl% = arrayttl% + 1 'if entry is not empty, add one to total
NEXT ctr

PRINT "You have"; arrayttl%; " items."

...instead, without using LBOUND and UBOUND?
Quote:Couldn't you use something like...

Code:
DIM inventory$(10) 'or whatever

FOR ctr = 1 to 10 'go through array
   IF inventory$(ctr) <> "" Then arrayttl% = arrayttl% + 1 'if entry is not empty, add one to total
NEXT ctr

PRINT "You have"; arrayttl%; " items."

...instead, without using LBOUND and UBOUND?
In your example, you arbitrarily chose 10 as the upper limit of your array. Based on the next bit of code, you can have less than 10 elements of the array containing any data. Given these conditions, the LBOUND and UBOUND will of course not tell you how many elements are occupied, and therefore, your coded approach is correct.

*****
?BOUND works on an array, not it's elements. If the number of (valid) elements in the array is potentially less than the size of the array, then you'll need a loop. If you're REDIM'ing the array to best fit the number of (valid) elements, ?BOUND works much cleaner.
"?BOUND" ? :normal:
LBOUND and UBOUND. "?" as a wildcard usually means "match one character".
Yeah, never seen it used like that though. I remember renaming files in DOS mode to have "?" and not being able to delete them in win3.1...