-
some arrays goodies
some array goodies
some features available only in thinbasic
Dim SomethingValue(500) As Long
SomethingValue(31)=31,32,33,34,35,36,37,38,39,40
the array from item 31 to 40 filled with these numbers
we can also use :
Array Assign SomethingValue(31)=31,32,33,34,35,36,37,38,39,40
Dim TextMatrix() As String 'dynamic string array
myText = "12_345_6789"
nLines = Parse(myText, TextMatrix, "_")
For i=1 To UBound(TextMatrix)
PrintL TextMatrix(i)
Next
the parse results fill the array items neatly without any extra code: note this :nLines = Parse(myText, TextMatrix, "_")
there are too many other goodies i will post from time to time
If you have other goodies about arrays you can add here
the complete example:
Code:
Uses "console"
Dim TextMatrix(), myText, nLines As String
Dim SomethingValue(500) As Long
Long i
SomethingValue(1)=1,2,3,4,5,6,7,8,9,10
SomethingValue(31)=31,32,33,34,35,36,37,38,39,40
Array Assign SomethingValue(401)=401,402,403,404,405
For i = 1 To 20
PrintL "index = "+Str$(i)+" "+SomethingValue(i)
Next
PrintL "******************************************"
For i = 31 To 40
PrintL "index = "+Str$(i)+" "+SomethingValue(i)
Next
PrintL "******************************************"
For i = 401 To 405
PrintL "index = "+Str$(i)+" "+SomethingValue(i)
Next
PrintL "******************************************"
myText = "12_345_6789"
nLines = Parse(myText, TextMatrix, "_")
For i=1 To UBound(TextMatrix)
PrintL TextMatrix(i)
Next
WaitKey
-
Hi,
Thanks for introducing these features in TB.
-
Primo,
did you know you can easily list array values without for cycle? Check this:
Code:
long SomethingValue(500)
SomethingValue(1)=1,2,3,4,5,6,7,8,9,10
SomethingValue(31)=31,32,33,34,35,36,37,38,39,40
SomethingValue(401)=401,402,403,404,405
msgbox 0, join$(SomethingValue, ",", "0", 1, 10)
msgbox 0, join$(SomethingValue, ",", "0", 31, 40)
msgbox 0, join$(SomethingValue, ",", "0", 401, 405)
Petr
-
:D
I like those ... challenges
Here we have "virtual variable" usage. Fill an array, read data as a matrix using a "virtual" variable
Virtual variable are variable that do not allocate/deallocate any memory but use memory allocated for other variables to read/write data (also in different ways)
Code:
'---An array of 9 elements
Long SomeValue_Array(9)
'---A matrix of 9 elements that share the same memory of the memory allocated for the array
Long SomeValue_Matrix(3, 3) At VarPtr(SomeValue_Array(1))
'---Fill the array
SomeValue_Array(1)=1,2,3,4,5,6,7,8,9
'---Read as matrix
MsgBox 0, Join$(SomeValue_Matrix, ",", $CRLF)
Have fun.
Eros
-
Thanks Petr and Eros, i was sure there are many golden eggs in thinbasic.
the classic programmer (like me) who are grown with QB or VB find these shortcuts and tips very strange, but it is in fact very useful and handy and time saving.
thanks
-
Yes absolutly true. Many many thanks
Kind regards,
Axel:D
-
Last day I had a little discussion on array ReDim in facebook with Petr Schreiber. My question is;
Assume that i am going to fill an array from an infinite source. (By infinite source, i mean, i don't know how many elements are there to fill my array.) So i have declared an array with one element and use ReDim in each time i put something to that array. i.e, Put something to that array and use ReDim to increase the size. Well, I know this will slow down my program execution. Then the other way which i know is, to declare a bigger array. say, 500 elements. In that case, what if my array filling operation completes in 200 elements ? Do i need to check the last element's position and delete the rest ?. I am seeking suggestion on this matter. Thanks in advance.
-kcvinu
-
It depends on data types and if array is one dimension or multiple dimensions (matrix).
With a single dimension array I would allocate, let's say, one thousand or one million elements.
Than fill the elements I need keeping track of the last.
If I need more, again Redim Preserve ... to other one thousand or one million elements.
At the end Redim Preserve ... to the last filled element.
You need to keep REDIM to the minimum because every REDIM is slower depending on how much memory is allocated and need to be reallocated, copied and free.
Another option is to use an Hash Table: http://www.thinbasic.com/public/prod...hash_table.htm
If you use the array index number for the hash table key, is like having an array but with the flexibility of an hash table that dynamically allocates data when needed.
http://www.thinbasic.com/public/prod...l?hash_set.htm
But again an Hash Table needs to have an idea on how many elements you need to create in order be be able to calculate hash values and reduce duplicated hash.
Another option is to use and AVL Tree.
No need to know number of elements in advance, just add data with a key easy to be retrieve.
http://www.thinbasic.com/public/prod...l?tree_avl.htm
There are examples in \thinBasic\SampleScripts\DataStructures\ using Hash Tables and AVL Trees that fill/search/delete data in a blink of an eye
-
i find this array strange (but very useful and handy) , it is used by Parse:
Dim NumbersMatrix() As Long
this syntax is not available in classic Basics
and then prepare the string myText
and then
nLines = Parse(myText, NumbersMatrix, "_")
it dimensions the NumbersMatrix as necessary according to the number of "_" in the myText, and since the MyText contains numbers and the NumbersMatrix() is of type Long then every array cell will have the number 12 or whatecer separated by "_"
i think it is possible to accept data (from mars as an example) as long string separated by space or "_" and then parse it to an array.
here is big string of one million numbers added to one million array cells (created internally by Parse)
i hope my talk about parse is correct .
Code:
Uses "console"
Dim NumbersMatrix() As Long
Dim myText As String
myText = Repeat$(1000000, "12_")
Long i, nLines
nLines = Parse(myText, NumbersMatrix, "_")
For i=990000 To UBound(NumbersMatrix)-1
PrintL Str$(i)+" "+Str$(NumbersMatrix(i)*2)
Next
WaitKey
-
@Eros,
Wow! AVL is superb.. very fast. Really, I love to code in thinBasic but now i can only do some experiments. Because all my projects are demanding unicode support. So eagerly waiting for the latest thinBasic version. Till that time, i would like to play with these kind of features. Thanks. :)