Qbasicnews.com
October 25, 2021, 11:03:27 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Counting arrays?  (Read 2791 times)
neuauld
New Member

Posts: 4


« on: June 18, 2006, 06:00:58 PM »

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
Logged
stylin
Ancient QBer
****
Posts: 445


« Reply #1 on: June 18, 2006, 06:27:30 PM »

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

stylin:
WaffleMan
Member
*
Posts: 40



« Reply #2 on: June 18, 2006, 06:55:19 PM »

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

url=http://www.freewebs.com/boxtopstuff/]Planet Boxtop[/url] (Look out for the redesign!)
The only member of QBNF with severe "tomorrow syndrome."
Moneo
Na_th_an
*****
Posts: 1971


« Reply #3 on: June 19, 2006, 12:16:15 AM »

Quote from: "WaffleMan"
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.

*****
Logged
stylin
Ancient QBer
****
Posts: 445


« Reply #4 on: June 19, 2006, 02:19:45 AM »

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

stylin:
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #5 on: June 19, 2006, 02:59:46 PM »

"?BOUND" ?  :normal:
Logged

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.
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #6 on: June 19, 2006, 05:51:17 PM »

LBOUND and UBOUND.  "?" as a wildcard usually means "match one character".
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #7 on: June 19, 2006, 11:12:46 PM »

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

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!