Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: some arrays goodies

  1. #1

    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:
    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
    
    Last edited by primo; 20-02-2017 at 17:30.

  2. #2
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    Hi,
    Thanks for introducing these features in TB.

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Primo,

    did you know you can easily list array values without for cycle? Check this:
    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
    Last edited by Petr Schreiber; 23-02-2017 at 11:05.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10


    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)
    '---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
    Last edited by ErosOlmi; 23-02-2017 at 13:49.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    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

  6. #6
    Yes absolutly true. Many many thanks

    Kind regards,

    Axel

  7. #7
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    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

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    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 .
    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
    
    Last edited by primo; 02-03-2017 at 19:46.

  10. #10
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    @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.

Page 1 of 3 123 LastLast

Similar Threads

  1. 3d arrays?
    By JosephE in forum thinDebug
    Replies: 2
    Last Post: 08-08-2010, 23:21
  2. Arrays
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 16:09

Members who have read this thread: 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •