Posts: 4
Threads: 2
Joined: Jun 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
Posts: 324
Threads: 12
Joined: Nov 2005
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.
stylin:
Posts: 40
Threads: 9
Joined: May 2005
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?
url=http://www.freewebs.com/boxtopstuff/]Planet Boxtop[/url] (Look out for the redesign!)
The only member of QBNF with severe "tomorrow syndrome."
Posts: 1,956
Threads: 65
Joined: Jun 2003
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.
*****
Posts: 324
Threads: 12
Joined: Nov 2005
?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.
stylin:
Posts: 3,368
Threads: 195
Joined: Jan 2003
"?BOUND" ? :normal:
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.
Posts: 1,439
Threads: 15
Joined: Apr 2003
LBOUND and UBOUND. "?" as a wildcard usually means "match one character".
Posts: 3,368
Threads: 195
Joined: Jan 2003
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...
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.