Author |
Topic: Passing Arrays (Read 3474 times) |
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Passing Arrays
« Reply #15 on: Mar 14th, 2014, 8:49pm » |
|
on Mar 14th, 2014, 6:11pm, JosephE wrote:| I edited the wiki page on arrays to mention that limitation for the time being. |
|
Many thanks for the valuable contributions to the Wiki.
Richard.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline


Gender: 
Posts: 35
|
 |
Re: Passing Arrays
« Reply #16 on: Mar 14th, 2014, 10:36pm » |
|
You bet! I've been learning a lot about LBB and I figured I'd help you document it.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
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: 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.
|
|
|
|
JosephE
New Member
member is offline


Gender: 
Posts: 35
|
 |
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.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
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.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline


Gender: 
Posts: 35
|
 |
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
|
|
Logged
|
|
|
|
bluatigro
Full Member
member is offline


Gender: 
Posts: 111
|
 |
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 ?
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
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.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline


Gender: 
Posts: 35
|
 |
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.
|
|
Logged
|
|
|
|
JosephE
New Member
member is offline


Gender: 
Posts: 35
|
 |
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 » |
Logged
|
|
|
|
|