Subs, functions, indexing arrays

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Tontoinjun
Newbie
Posts: 2
Joined: Mon Oct 31, 2016 6:33 pm

Subs, functions, indexing arrays

Post by Tontoinjun »

I have been trying to write a sub or function that will return the index number of a 1 dim array of strings when a string I is submitted and a match is found. The problem is submitting a string and returning an integer.

Input " name"; name
For I = 1 to 32
If name = namearray(I)
Then index = I
Next

How do you put in a sub or function?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Subs, functions, indexing arrays

Post by burger2227 »

Code: Select all

DIM SHARED namearray(1 TO 32) AS STRING 'shared array will need names put into it earlier with DATA or file input

Input " name"; name$
position% = index%(name$)
IF position% THEN PRINT name$; position%

FUNCTION index%(n$)
For I = 1 to 32
  If n$ = namearray(I) Then index% = I : EXIT FUNCTION ' exits at first name match
Next ' exits with last name match position or 0
END FUNCTION
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Tontoinjun
Newbie
Posts: 2
Joined: Mon Oct 31, 2016 6:33 pm

Re: Subs, functions, indexing arrays

Post by Tontoinjun »

Thank you! That solved the problem that I have racked my brain over. What a relief. Thanks again.
Post Reply