Author |
Topic: Passing Arrays (Read 3478 times) |
|
JosephE
New Member
member is offline


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


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


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


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


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


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


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


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


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


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


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