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

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

Subs, functions, indexing arrays

Postby Tontoinjun » Mon Oct 31, 2016 7:26 pm

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: 2144
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Subs, functions, indexing arrays

Postby burger2227 » Sat Nov 05, 2016 9:16 am

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 : http://www.QB64.NET
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

Postby Tontoinjun » Fri Nov 11, 2016 8:04 pm

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


Return to “QBASIC and QB64 Questions & Answers”

Who is online

Users browsing this forum: No registered users and 6 guests

cron