LB Booster
« Passing Arrays »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 05:09am



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 3498 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #17 on: Mar 16th, 2014, 11:08am »

on Mar 13th, 2014, 9:20pm, JosephE wrote:
Code:
' Simple Array Functions
' by Joseph Essin 

If you are working with arrays, you will be interested to know that LBB supports some whole-array arithmetic operations, for example adding two arrays together without needing to code a loop:

Code:
    a() = b() + c() 

Previously this facility has been undocumented (frankly I had forgotten that it was supported!) but now I've written a Wiki article listing the various things you can do:

http://bb4w.wikispaces.com/Array+arithmetic

Richard.
« Last Edit: Aug 21st, 2015, 9:26pm by Richard Russell » User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #18 on: Mar 16th, 2014, 10:41pm »

on Mar 16th, 2014, 11:08am, Richard Russell wrote:
If you are working with arrays, you will be interested to know that LBB supports some whole-array arithmetic operations, for example adding two arrays together without needing to code a loop


Wow, that's amazing. And very handy.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #19 on: Mar 22nd, 2014, 10:52pm »

on Mar 13th, 2014, 9:20pm, JosephE wrote:
I'm trying to make a few functions to do a redim preserve

The code below now works; it requires LBB v2.53, and the BYREF is essential to allow the array to be redimensioned inside the function/sub:

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 byref 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 

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #20 on: Mar 23rd, 2014, 11:33pm »

on Mar 22nd, 2014, 10:52pm, Richard Russell wrote:
The code below now works; it requires LBB v2.53, and the BYREF is essential to allow the array to be redimensioned inside the function/sub:


Wow, you've been busy! Thank you smiley
User IP Logged

bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: Passing Arrays
« Reply #21 on: Feb 17th, 2015, 09:56am »


pasing arrays into subs and functions
ok thats nice

but how do i make a function that outputs a array ?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #22 on: Feb 17th, 2015, 11:47am »

on Feb 17th, 2015, 09:56am, bluatigro wrote:
but how do i make a function that outputs a array ?

You pass the array BYREF, as follows:

Code:
    call mySub myArray()
    print myArray(50)
    end

sub mySub byref a()
    dim a(100)
    a(50) = 12345
end sub 

Here myArray() does not yet exist when the sub is called; the local array a() is created inside the sub and then passed back to the caller as myArray(). On return from the sub a() ceases to exist.

LB 4 doesn't have any functionality like this; arrays are always global so it is impossible to write a general-purpose function which operates on any specified array. This is in my opinion a major weakness.

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #23 on: Mar 1st, 2015, 03:07am »

Just wanted to follow up and say thanks for your help. The ability to re-dimension arrays and preserve their contents is very useful. smiley
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #24 on: Mar 1st, 2015, 03:36am »

In fact, here's an up-to-date version of the code that supports scaling of string and numeric arrays:

Code:
' Simple Array Functions
' by Joseph Essin and Richard Russell
'
' 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." Richard even customized LBB
' to allow these user-defined functions to work.
'
' 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 ArrayLib$(0)
' -------------------------------------------------------------- '
function nsize(array())
  ' Returns the upper index limit of the numeric array array().
  s = 2^24
  do
    on error goto [Error]
    [Error]
    s = int(s / 2)
    temp = array(nsize + s)
    nsize = nsize + s
  loop until s = 0
end function

function size(array$())
  ' Returns the upper index limit of the string array array$().
  s = 2^24
  do
    on error goto [Error]
    [Error]
    s = int(s / 2)
    temp$ = array$(size + s)
    size = size + s
  loop until s = 0
end function

sub preserve byref array(), newSize
	' Redimensions the given one-dimensional numeric array to the
	' specified newSize.
	u = nsize(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
	next i
end sub

sub preserve$ byref array$(), newSize
	' Redimensions the given one-dimensional string array to the
	' specified newSize.
	u = size(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
	next i
end sub 
« Last Edit: Mar 1st, 2015, 03:38am by JosephE » 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