Qbasicnews.com
Counting arrays? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: Counting arrays? (/thread-9433.html)



Counting arrays? - neuauld - 06-19-2006

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


Counting arrays? - stylin - 06-19-2006

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.


Counting arrays? - WaffleMan - 06-19-2006

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?


Counting arrays? - Moneo - 06-19-2006

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.

*****


Counting arrays? - stylin - 06-19-2006

?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.


Counting arrays? - Agamemnus - 06-19-2006

"?BOUND" ? :normal:


Counting arrays? - DrV - 06-20-2006

LBOUND and UBOUND. "?" as a wildcard usually means "match one character".


Counting arrays? - Agamemnus - 06-20-2006

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...