|
Post by arryman on Jan 20, 2022 17:03:49 GMT -5
In some Basics, it's possible to interrogate an array to find its size. Something like :"print sizeof(array1)" could return "100" etc. How do I do that in LB 4.5.1.?
|
|
|
Post by Carl Gundel on Jan 20, 2022 19:12:44 GMT -5
In some Basics, it's possible to interrogate an array to find its size. Something like :"print sizeof(array1)" could return "100" etc. How do I do that in LB 4.5.1.? You can't, but if you write a program that dimensions an array then you already know the size of the array. Or am I missing something? -Carl
|
|
|
Post by Brandon Parker on Jan 20, 2022 21:15:32 GMT -5
Feel free to jump over to my Import Architect forum and check out where the Import Architect and the Dynamic Array library may be of use to you. {:0) Brandon Parker
|
|
|
Post by tsh73 on Jan 21, 2022 3:43:39 GMT -5
Also since arrays is global in LB, you cannot pass them in subs/functions and get array you don't know about.
|
|
|
Post by arryman on Jan 21, 2022 5:20:29 GMT -5
Thanks for all the replies. I'm writing a program to emulate lists, a feature I don't believe is available in LB. It uses REDIM a lot, and while I can keep track of the size with a variable, it would have been helpful in places to simply use something like "sizeof." Needless to say, I can manage without it, but it would have been a "nice to have" feature in this instance.
|
|
|
Post by arryman on Jan 21, 2022 5:22:24 GMT -5
By the way, although Python features lists heavily, it works best with strings. There are certain things you can't do with integers in Pythonic lists. That's why I've returned to LB, forgetting initially that it doesn't do lists.
|
|
|
Post by tsh73 on Jan 21, 2022 8:16:11 GMT -5
I have a user-defined function Ubound(arrayName$) stored on my HD since 2015
I admit I cannot expaln how it works. But it definitely does.
'find array size by binary search
dim b(73561) print UBound("b") 'should be 73561
for n = 1 to 1000 dim a(n) m = UBound("a") if m<>n then print "Ubound failed at ";n: end next 'else print "For arrays 1..1000 Ubound returned right value"
Function UBound(arrName$) ' 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) 'print s temp = eval(arrName$;"(";UBound + s;")") UBound = UBound + s 'print s, UBound loop until s = 0 End Function
|
|
|
Post by Carl Gundel on Jan 21, 2022 11:11:48 GMT -5
By the way, although Python features lists heavily, it works best with strings. There are certain things you can't do with integers in Pythonic lists. That's why I've returned to LB, forgetting initially that it doesn't do lists. You used to be a Liberty BASIC programmer and now you're back? Cool. What kind of project are you working on? Perhaps you will find the dynamic dictionary stuff I blogged about useful. basicprogramming.blogspot.com/2016/Scroll down to find the dictionary posts.
|
|
|
Post by Brandon Parker on Jan 21, 2022 17:14:50 GMT -5
I have a user-defined function Ubound(arrayName$) stored on my HD since 2015 I admit I cannot expaln how it works. But it definitely does. It is just starting from 16,777,216, halving on each iteration until it gets below the dimensioned array size and the array element becomes valid. It then starts building back up halving the "s" variable repeatedly until the "UBound + s" becomes valid again. It keeps repeating this cycle until s = 0 at which point the UBound has converged to the correct dimensional size of the array. That does limit the size of the array to ((2^24) - 1) elements though. If you print 2^24 out at the start of the program and then dimension the array to 2^24, you will see that the function returns an incorrect upper limit. Obviously, you could just set the "s" variable within the function to a higher value (2^x) and it would work up to that minus 1, but it provides no facility to tell you that your array dimension is beyond the value of "s". Also, if interested, the Import Architect does provide Singly & Doubly Linked List capabilities, but they too are limited to strings being stored within them. That being said, LB makes it very easy to manipulate data after it is retrieved. Just shamelessly plugging here; if anyone is interested hop over to the forum and say hello. {:0) Brandon Parker
|
|
|
Post by arryman on Jan 22, 2022 5:16:00 GMT -5
Thanks to Anatoly and Brandon for the code and explanation. I can see it will work, albeit somewhat "heavy." My remarks about python lists and numerals is only a problem in 2 or more dimensions. Everything is fine in one dimension.
In answer to Carl's query, I'm working on an amateur radio logging program which I started in python but then ran into the list problem. My method of solving it in LB when wanting to expand the "list" is to copy its contents into another array, redim the original array, and copy the original contents back to the now-expanded array. It seems clumsy, but modern computers are so fast and have enough memory to make this happen faster than you can blink.
The original problem can be solved in python by importing a very complex library called numpy, which is not a python original, to do the heavy lifting. It all seems so complicated to solve such a simple problem! I couldn't do it now, but in the early days of PCs I used to program in machine code (Z80 = easy) and that would have been the ideal solution.
|
|
|
Post by Rod on Jan 22, 2022 7:29:53 GMT -5
Without thinking it through too much I would say an array is the wrong way to go. You probably want a linked list file which Liberty can do relatively easily. So no array involved you just have list elements that are linked. Each element knows the location of its previous element and the location of its following element. The file is not actually in any specific order but the links form the order. It can be expanded simply by adding to the end. This append can be inserted into a chain of links without any need of copying or moving elements.
If you were able to post the data model we might see if the concept suits your application.
|
|
|
Post by arryman on Jan 23, 2022 11:19:04 GMT -5
Rod, I already know the data model, and it needs a linked list. How do I do that in LB? Any info gratefully received.
|
|
|
Post by Brandon Parker on Jan 23, 2022 11:37:07 GMT -5
Linked Lists are not a native data structure in Liberty BASIC.
So, you will either have to find another route, which should be doable or, hop over to my forum and we can discuss your program and how the Import Architect can help you achieve what you want with a Singly or Doubly Linked List.
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Jan 23, 2022 13:55:10 GMT -5
Well, instead of pointers to memory LB does not readily provide you can make two arrays of big enough size One is "value" part, any type/columns you like, say items$(bigEnough) Another is "pointer" part, numeric array which holds number of "next" item in items$() array. (you can store not only "next" but also "previous" link making double-linked list).
|
|
|
Post by Rod on Jan 23, 2022 15:23:46 GMT -5
This is the last code I posted that uses some of the concepts we are discussing. It is a contact management tool. It uses linked lists, RAF files and index files. Arrays are used when needed. Have a browse some of the techniques may strike a chord. libertybasiccom.proboards.com/thread/878/simple-contacts-database But to set it all in context for your application and to show you some code examples we really need to see your data model so that we can suggest how to structure storage and retrieval. A linked list can be engineered in many ways, Liberty has no native functions but they are easily written.
|
|