PDA

View Full Version : Array Join



ReneMiner
23-11-2015, 19:51
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

ErosOlmi
25-11-2015, 19:00
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

ReneMiner
25-11-2015, 19:16
...
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.

ErosOlmi
25-11-2015, 19:41
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 :suicide:

ReneMiner
26-11-2015, 00:09
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.