LB Booster
Programming >> Liberty BASIC language >> passing arrays to subs
http://lbb.conforums.com/index.cgi?board=lblang&action=display&num=1499617046

passing arrays to subs
Post by Alincon on Jul 9th, 2017, 4:17pm

I need to load about 20 small tables at the beginning of my program.
How can I pass array names, one at a time, to the sub, instead of using 20 if statements, as I have done here?
r.m.

Code:
 open "C:\LibBas\_MyLibBas\matchmaker\tables.txt" for input as #tbl
    for j = 1 to 3
        call getTables j
    next  
   close #tbl
   end

 sub getTables j
    input #tbl,x$ 
    n=1
    while word$(x$,n) <> ""
        if j = 1 then religion$(n) = word$(x$,n)
        if j = 2 then race$(n) = word$(x$,n)
        if j = 3 then pet$(n) = word$(x$,n)
        ' 17 more ifs
        n=n+1
    wend
    end sub

 


Tables.txt:
Code:
Buddist Catholic Muslim Mormon Protestant Other None
White Black Asian Polynesian Hispanic
Birds Cats Dogs Fish 
 

Re: passing arrays to subs
Post by tsh73 on Jul 9th, 2017, 7:29pm

Passing arrays to SUBs and FUNCTIONs

which gives us
Code:
 open "C:\progs\LBB\tables.txt" for input as #tbl
'    for j = 1 to 3
'        call getTables j
'    next  
    call getTable religion$()
    call getTable race$()
    call getTable pet$()
   close #tbl
   
   call showArray religion$()
   call showArray race$()
   call showArray pet$()

   end

sub getTable arr$()
    input #tbl,x$ 
    n=1
    while word$(x$,n) <> ""
        arr$(n) = word$(x$,n)
        n=n+1
    wend
end sub    
    
sub showArray arr$()
    for i = 1 to 10
    print arr$(i);" ";
    next
    print
end sub    
    
 sub getTables j
    input #tbl,x$ 
    n=1
    while word$(x$,n) <> ""
        if j = 1 then religion$(n) = word$(x$,n)
        if j = 2 then race$(n) = word$(x$,n)
        if j = 3 then pet$(n) = word$(x$,n)
        ' 17 more ifs
        n=n+1
    wend
    end sub
     

Re: passing arrays to subs
Post by Alincon on Jul 9th, 2017, 11:45pm

Too bad this does not work. I have to make 20 call statements.
Code:
  tableName$(1)="religion$()":tableName$(2)="race$()":tableName$(1)="pet$()"

    for j = 1 to 3
        call getTable tableName$(j)
    next

 

Re: passing arrays to subs
Post by tsh73 on Jul 10th, 2017, 06:47am

Search turned this thread
array operations

With that, we have
Code:
tableName$(1)="religion$"
tableName$(2)="race$"
tableName$(3)="pet$"

open "C:\progs\LBB\tables.txt" for input as #tbl

    for j = 1 to 3
        call getTable tableName$(j)
    next

   close #tbl

   call showArray religion$()
   call showArray race$()
   call showArray pet$()

print "--------------"
   end

function assignStr(byref a$, b$)
    a$ = b$
end function

sub getTable arrName$
    input #tbl,x$
    n=1
    while word$(x$,n) <> ""
        value$=word$(x$,n)
        'print "FNassignStr(";arrName$;"(";n;"),value$)"
        dummy = eval("FNassignStr(";arrName$;"(";n;"),value$)")
        n=n+1
    wend
end sub

sub showArray arr$()
    for i = 1 to 10
    print arr$(i);" ";
    next
    print
end sub

 

Re: passing arrays to subs
Post by Richard Russell on Jul 10th, 2017, 08:25am

on Jul 9th, 2017, 11:45pm, Alincon wrote:
Too bad this does not work. I have to make 20 call statements.

Assigning the 20 array names to the elements of tableName$() is just as much code as making 20 call statements, probably more! In fact from a code readability standpoint I would have thought that the multiple call statements were clearer. So I'm not sure that I can see what the benefit would be in this particular case.

Richard.

Re: passing arrays to subs
Post by Alincon on Jul 10th, 2017, 2:20pm

I have made some progress, but still not what I want:
Code:
 'tables.txt now includes the array names on a separate line before the array items
    open "C:\LibBas\_MyLibBas\matchmaker\tables.txt" for input as #tbl
    for j = 1 to 4
        input #tbl, xx$   'get array name      
        print xx$
        call getTable xx$()  
    next
    
   for j = 1 to 7     'checking the arrays
        print j,religion$(j),race$(j),pet$(j),television$(j)  'shows only blanks
   next
   close #tbl  
   end

 sub getTable arr$() 
    input #tbl,x$    'get array item
    n=1
    while word$(x$,n) <> ""
        arr$(n) = word$(x$,n)    'this seems to work 
        print, arr$(n)  'show the array item
        n=n+1
    wend
    end sub

 


revised tables.txt file:
Code:
religion$
Buddist Catholic Muslim Mormon Protestant Other None
race$
White Black Asian Polynesian Hispanic
pet$
Birds Cats Dogs Fish 
television$
Drama Comedy Game Cops Mystery Sci-Fi Reality
 

r.m.
Re: passing arrays to subs
Post by tsh73 on Jul 10th, 2017, 3:00pm

Alincon,
I wonder if you actually READ answers?

use
sub getTable arrName$ / function assignStr(byref a$, b$)
from Reply#3.
Re: passing arrays to subs
Post by Richard Russell on Jul 10th, 2017, 4:00pm

on Jul 10th, 2017, 2:20pm, Alincon wrote:
revised tables.txt file:

I would suggest you step back and consider an alternative way of handling this. For example could not the category names like 'religion', 'race' etc. themselves be data items? Something like this:

Code:
category$(1) = "religion" : category$(2) = "race" : category$(3) = "pet"
count(1) = 7 : count(2) = 5 : count(3) = 4
name$(1,1) = "Buddist" : name$(1,2) = "Catholic" : ... etc.
name$(2,1) = "White" : name$(2,2) = "Black" : name$(2,3) = "Asian" : ... etc.
name$(3,1) = "Bird" : name$(3,2) = "Cat" : name$(3,3) = "Dog" : ... etc. 

Now everything is in three arrays: category$(), count() and name$() and you should be able to do whatever you want using indexing.

Richard.