LB Booster
« passing arrays to subs »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 04:17am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: passing arrays to subs  (Read 276 times)
Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx passing arrays to subs
« Thread started 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 
 
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: passing arrays to subs
« Reply #1 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
     
User IP Logged

Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx Re: passing arrays to subs
« Reply #2 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

 
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: passing arrays to subs
« Reply #3 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

 
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: passing arrays to subs
« Reply #4 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.
« Last Edit: Jul 10th, 2017, 08:27am by Richard Russell » User IP Logged

Alincon
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 147
xx Re: passing arrays to subs
« Reply #5 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.
User IP Logged

tsh73
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: passing arrays to subs
« Reply #6 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.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: passing arrays to subs
« Reply #7 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.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls