Results 1 to 5 of 5

Thread: Array Join

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170

    Array Add

    small idea for

    Long lCount = Array Add arrayvariable2 Into arrayvariable1


    in example below array sArray1() will thereafter contain all the members of sArray1() and sArray2() together.
    sArray2 is empty then.

    i needed a few times something as this to fast join 2 arrays of strings,


    this only works for dynamic string-array, other types should be easy to realize...
    could also be Add$ for Strings & Add for other types but i guess thinCore will know the type...

    maybe something for new Keyword Into,
    optional switches as Unique or Ascend/Descend thinkeable

    Array Add [Unique] v2 [,Collate Ucase][,Ascend | Descend] Into v1



    Uses "console"
    
    ' --------------------------------------------------------------------
    
    Function Array_Add(ByRef a1() As String, _
                       ByRef a2() As String  _
                        ) As Long
    ' --------------------------------------------------------------------  
      
      ' will move all elements of a2 into a1
      ' a2 will be empty thereafter
      ' returns Ubound of a1
      
      
      If UBound(a2) = 0 Then Return Ubound(a1)
      
      
      ReDim Preserve a1(UBound(a1) + UBound(a2))
      
       Memory_Swap(                                        _
          VarPtr(a1(1)) + 4 * ( UBound(a1) - UBound(a2) ), _
          VarPtr(a2(1)),                                   _
          4 * UBound(a2)                                   )
       
      Function = UBound(a1)
      
    
    End Function  
    
    ' now test it:
    ' --------------------------------------------------------------------
    Function TBMain() 
    ' --------------------------------------------------------------------
      
      Local sArray1(3) As String
      Local sArray2(5) As String
      Local i          As Long
      
      For i = 1 To UBound(sArray1)
        sArray1(i) = "array1(" & TStr$(i) & ")"
      Next
      
      For i = 1 To UBound(sArray2)
        sArray2(i) = "array2(" & TStr$(i) & ")"
      Next
      
      i = array_Add(sArray1, sArray2)
      
      While i                            
        i -= 1
        PrintL sArray1(UBound(sArray1) - i )
      Wend          
      For i = 1 To UBound(sArray2)
        PrintL i, sArray2(i)
      Next
      
      PrintL "key to end"
      WaitKey
    End Function
    
    Last edited by ReneMiner; 25-11-2015 at 18:45. Reason: changed from Join to Add
    I think there are missing some Forum-sections as beta-testing and support

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

    does your code can be summarized by the following code:
    Parse Join$(sArray2, $NUL), sArray1, $NUL
    
    Not really optimized and quite slow for big arrays because there are a lot of internal string handling.

    Anyway, more ARRAY functions are always welcome.
    I will think about your request in particular on how to generalize.

    Ciao
    Eros
    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

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    Quote Originally Posted by ErosOlmi View Post
    ...
    does your code can be summarized by the following code:
    Parse Join$(sArray2, $NUL), sArray1, $NUL
    
    ...
    Nope, that is not what it does:
    I want the members of array B() to become additional new members of array A().

    in Basic:
    Local sArray1(3) As String ' 1 array of strings
    Local sArray2(5) As String ' another String-array
    Local i, lOld       As Long  ' little helpers
      
    For i = 1 To UBound(sArray1)
      sArray1(i) = "array1(" & TStr$(i) & ")"  ' assign identity
    
    Next
      
    For i = 1 To UBound(sArray2)
      sArray2(i) = "array2(" & TStr$(i) & ")" ' assign identity
    
    Next
    
    lOld = Ubound(sArray1)  ' store old count of elements in sArray1
    
    ' resize sArray1 to have enough space for th new members:
    
    Redim Preserve sArray1(Ubound(sArray1) + Ubound(sArray2))
    
    ' transfer the members of sArray2 to be members of sArray1 in future:
    For i = 1 To Ubound(sArray2)
     sArray1(i+lOld) = sArray2(i)
    Next
    ' now sArray1 holds all the members of both arrays,
    ' sArray2 i dont need any more...
    ' next step would be to sort the new sArray1() ...
    
    ' practical use was, to join lists of tB-Keywords and 
    ' User-tokens that shall be displayed together in autocomplete...
    
    this was the intention.
    Last edited by ReneMiner; 25-11-2015 at 19:25.
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    This way
    Parse Join$(sArray1, $NUL) & $NUL & Join$(sArray2, $NUL), sArray1, $NUL
    
    Sorry, just joking, I'm too tired today due to work load.
    Already worked 12 hours and still to have 2 hours drive back to home and some work to finish from home.

    It cannot continue this way
    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
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    i wish i could do some of your work, so you have more time to develop new functions ,

    we have a keyword Add already so i thought it would be the appropriate one. Join was almost the correct keyword (thats the reason why the thread is entitled Array Join)

    but i should have suggested this:

    Array Merge [Unique]array2[,array3[,... ?]] Into array1


    the first function at the top does almost that: it swaps the stringpointers from array2 with the new stringpointers of just dimensioned new members of array1. Array1 has its members and they will persist. Mandatory only: all arrays to merge have to be of the same type, 1-dimensional.

    Last edited by ReneMiner; 27-11-2015 at 22:56.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Help file: JOIN$ sample
    By Petr Schreiber in forum Fixed or cleared errors in help material
    Replies: 2
    Last Post: 23-10-2011, 15:44
  2. JOIN$ with matrix
    By ErosOlmi in forum thinBasic vaporware
    Replies: 0
    Last Post: 03-07-2010, 12:04
  3. Usage of the JOIN$ Keyword
    By Michael Clease in forum Samples for help file
    Replies: 2
    Last Post: 28-05-2007, 13:17

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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