LB Booster
« Passing Arrays »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:49am



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 2  Notify Send Topic Print
 hotthread  Author  Topic: Passing Arrays  (Read 3472 times)
JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Passing Arrays
« Thread started on: Mar 13th, 2014, 07:30am »

Can anyone provide an example of an array being passed to a function so I can see what the syntax for it is? smiley
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #1 on: Mar 13th, 2014, 09:21am »

on Mar 13th, 2014, 07:30am, JosephE wrote:
Can anyone provide an example of an array being passed to a function so I can see what the syntax for it is? :)

Here's a simple example:

Code:
    one(5) = 123
    call test one()
    end

sub test two()
    print two(5)
end sub 

To demonstrate that the array two() is genuinely 'local' to the SUB:

Code:
    one(5) = 123
    two(5) = 456
    call test one()
    print two(5)
    end

sub test two()
    print two(5)
end sub 

Arrays are automatically passed 'by reference' (you don't need to specify BYREF):

Code:
    one(5) = 123
    two(5) = 456
    call test one()
    print two(5)
    print one(5)
    end

sub test two()
    print two(5)
    two(5) = 789
end sub 

Of course you can use a FUNCTION instead of a SUB:

Code:
    one(5) = 123
    two(5) = 456
    print test(one())
    print two(5)
    print one(5)
    end

function test(two())
    test = two(5)
    two(5) = 789
end function 

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #2 on: Mar 13th, 2014, 8:35pm »

Richard, these are absolutely wonderful. Thank you so much.

I've written a simple UBound function to determine the upper-index limit of an array. However, it's acting funny. It's returning one more than it seems like it should. I'm wondering if it has to do with how I've implemented the On Error part.

Here's my code:

Code:
*incorrect code removed* 


Edit: After thinking about it, I realize why it does that. It seems the UBound function should simply be UBound = i - 1.

Code:
ReDim things(5) ' Make it hold five things

Print UBound(things())

End


Function UBound(array())
	' Returns the upper index limit of array().
	' ---------------------------------------------------------- '
	On Error Goto [Error]
	While 1
		temp = array(i)
		i = i + 1
	WEnd
	[Error]
		UBound = i - 1 ' Return value
End Function 
« Last Edit: Mar 13th, 2014, 8:42pm by JosephE » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #3 on: Mar 13th, 2014, 9:11pm »

on Mar 13th, 2014, 8:35pm, JosephE wrote:
I've written a simple UBound function to determine the upper-index limit of an array.

Nice. However it might be a bit slow with a large array. You could use a 'binary chop' to make it a lot faster:

Code:
Function UBound(array())
    ' Returns the upper index limit of array().
    ' ---------------------------------------------------------- '
    s = 2^24
    do
        On Error Goto [Error]
        [Error]
        s = int(s / 2)
        temp = array(UBound + s)
        UBound = UBound + s
    loop until s = 0
End Function 

An alternative approach would be to cheat and incorporate a little bit of BBC BASIC code:

Code:
Function UBound(array())
    !UBound = DIM(array(),1)
End Function 

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #4 on: Mar 13th, 2014, 9:20pm »

Richard, that's genius. I had no idea. I have seen binary chop methods before but didn't think to try implementing one.

I'm alright with cheating if it works, haha.

I'm trying to make a few functions to do a redim preserve, but I'm having trouble. Here's where I'm at. Note that it doesn't even output the last print statement:

Code:
' Simple Array Functions
' by Joseph Essin
' Feel free to use in your own code. No need to credit me.
'
' For use with LBB:
' http://www.bbcbasic.co.uk/lbb/
'
' The functionality to make this work is not found in
' Liberty BASIC alone. This is for use with Richard Russell's
' Liberty BASIC Booster "compiler."
'
' Please find LBB below:
'
' http://www.bbcbasic.co.uk/lbb/
' -------------------------------------------------------------- '

' These are necessary for the Array Library.
' Don't name any of your arrays these names
' or you'll be subject to data loss.
Redim ArrayLib(0)
' -------------------------------------------------------------- '


Redim things(5) ' Make it hold five things

For i = 1 To 5
	things(i) = 5^i
	Print i, things(i)
Next i

Print "UBound before redim preserve: "; UBound(things())

Call RedimPreserve things(), 10

Print "UBound after redim preserve: "; UBound(things())

End


Function UBound(array())
    ' Returns the upper index limit of array().
    ' ---------------------------------------------------------- '
    ' Special thanks to Richard for the implementation of the
    ' binary chop method.
    ' ---------------------------------------------------------- '
    s = 2^24
    do
        On Error Goto [Error]
        [Error]
        s = int(s / 2)
        temp = array(UBound + s)
        UBound = UBound + s
    loop until s = 0
End Function



Sub RedimPreserve array(), newSize
	' Redimensions the given one-dimensional array() to the
	' specified newSize.
    ' ---------------------------------------------------------- '
	' Array indexes begin at 0, so keep that in mind when
	' specifying newSize.
	' ---------------------------------------------------------- '
	u = UBound(array()) ' Get the upper limit
	Redim ArrayLib(u)
	For i = 0 To u
        ArrayLib(i) = array(i) ' Get old contents
	Next i
	Redim array(newSize) ' Resize given array.
	For i = 0 To u
        array(i) = ArrayLib(i) ' Replace old contents that were lost.
	Next i
End Sub 
« Last Edit: Mar 13th, 2014, 9:26pm by JosephE » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #5 on: Mar 13th, 2014, 9:55pm »

on Mar 13th, 2014, 9:20pm, JosephE wrote:
Note that it doesn't even output the last print statement

Something really screwy is happening there. Leave it with me and I'll try to figure it out. Sorry for the inconvenience. embarassed

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #6 on: Mar 13th, 2014, 9:56pm »

on Mar 13th, 2014, 9:55pm, Richard Russell wrote:
Something really screwy is happening there. Leave it with me and I'll try to figure it out. Sorry for the inconvenience.


Okay, thank you! There's no inconvenience. This is just my spring break hobby programming time XD
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #7 on: Mar 13th, 2014, 10:23pm »

on Mar 13th, 2014, 9:55pm, Richard Russell wrote:
Leave it with me and I'll try to figure it out.

Right, I'm awfully sorry but although LBB allows you to pass an entire array to a SUB or FUNCTION you can't REDIM it inside the function. Unfortunately it doesn't report an error if you try, but just crashes in a strange way as you have discovered.

So I'm afraid you won't be able to make a RedimPreserve function that way. sad

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #8 on: Mar 13th, 2014, 10:25pm »

Ah, okay, that's what I was afraid of. It's not a huge deal. smiley
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #9 on: Mar 14th, 2014, 12:34am »

on Mar 13th, 2014, 10:25pm, JosephE wrote:
Ah, okay, that's what I was afraid of. It's not a huge deal. smiley

It would be possible to modify LBB so RedimPreserve can work, but I am unsure whether the benefit justifies the effort. What do you (and others) think?

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #10 on: Mar 14th, 2014, 02:47am »

on Mar 14th, 2014, 12:34am, Richard Russell wrote:
It would be possible to modify LBB so RedimPreserve can work, but I am unsure whether the benefit justifies the effort. What do you (and others) think?


Obviously I would be in favor of that.

However, if you simply added the preserve functionality like in Visual BASIC (or any other syntax you prefer), I wouldn't even need to write the subroutine ;)

As in:
Code:
ReDim Preserve array(newSize) 
« Last Edit: Mar 14th, 2014, 02:48am by JosephE » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #11 on: Mar 14th, 2014, 05:15am »

on Mar 14th, 2014, 02:47am, JosephE wrote:
However, if you simply added the preserve functionality

It's not simple! For a start it would involve adding a new keyword PRESERVE which raises issues of compatibility with programs which might have used it as a variable name. Then there's the fact that BBC BASIC, which is the underlying technology behind LBB, doesn't support that functionality natively so it would have to be bodged using a technique similar to yours. And one would have to consider whether it was sensible to add REDIM PRESERVE without at the same time adding other array-related functionality such as a UBOUND function.

On the other hand tweaking LBB so that your technique actually works should be relatively straightforward and safe.

Richard.
« Last Edit: Mar 14th, 2014, 08:10am by Richard Russell » User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #12 on: Mar 14th, 2014, 08:49am »

on Mar 14th, 2014, 05:15am, Richard Russell wrote:
On the other hand tweaking LBB so that your technique actually works should be relatively straightforward and safe.


I can definitely live without it. But if you get bored, there's an idea haha.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #13 on: Mar 14th, 2014, 11:59am »

on Mar 14th, 2014, 08:49am, JosephE wrote:
I can definitely live without it. But if you get bored, there's an idea haha.

If I need to release a new version for another reason, perhaps to fix a bug, I will definitely incorporate the modification at the same time. If I'd known about your requirement a day earlier I could have included it in v2.52, but there you go.

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #14 on: Mar 14th, 2014, 6:11pm »

Okay, I edited the wiki page on arrays to mention that limitation for the time being. smiley
User IP Logged

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

| |

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