Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An hash array dictonary step by step
#22
(04-02-2023, 07:50 PM)bplus Wrote: OK you guys made me look at what I did with Dictionary, no hash but I did get multiple values for a given key:
Code: (Select All)
' note: I buggered this twice now, FOR base 1 array REDIM MyArray (1 to 1) AS ... the (1 to 1) is not same as (1) which was the Blunder!!!
'notes: REDIM the array(0) to be loaded before calling Split '<<<< IMPORTANT dynamic array and empty, can use any lbound though
'This SUB will take a given N delimited string, and delimiter$ and create an array of N+1 strings using the LBOUND of the given dynamic array to load.
'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given.  rev 2019-08-27
Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
    Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
    curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
    dpos = InStr(curpos, SplitMeString, delim)
    Do Until dpos = 0
        loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
        arrpos = arrpos + 1
        If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
        curpos = dpos + LD
        dpos = InStr(curpos, SplitMeString, delim)
    Loop
    loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
    ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
End Sub

I did use Split but for removing or modifying multiple items under a key. K stands for key and V stands for value(s).

Hey bplus, dictionary aside, I see you have an updated split function that preserves the lbound of the array. 
I've been using Luke's forever, but preserving lbound might be a nice feature. 
I tried testing both versions of split, but yours is blowing up on me. 
If you get a minute, could you give this a try? 

Code: (Select All)
ReDim MyArray(-1) As String
Dim MyString As String
Dim MyDelim As String
Dim iLoop As Integer

' =============================================================================
Cls
Print "Compare array split functions, test #1-B"
Print

MyString = "a,b,c,d,e,f"
MyDelim = ","
ReDim MyArray(-1) As String

Print "Before LukeSplit:"
Print "  ReDim MyArray(-1) As String:"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
Print "  MyString       =" + Chr$(34) + MyString + Chr$(34)
Print "  MyDelim        =" + Chr$(34) + MyDelim + Chr$(34)
Print

LukeSplit MyString, ",", MyArray()
Print "After LukeSplit MyString, " + Chr$(34) + "," + Chr$(34) + ", MyArray():"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
For iLoop = LBound(MyArray) To UBound(MyArray)
    Print "  MyArray(" + _Trim$(Str$(iLoop)) + ")=" + Chr$(34) + MyArray(iLoop) + Chr$(34)
Next iLoop
Print

Print "Press any key to continue.": Sleep: _KeyClear: '_Delay 1

' =============================================================================
Cls
Print "Compare array split functions, test #1-B"
Print

MyString = "g,h,i,j,k,l"
MyDelim = ","
ReDim MyArray(10 To 12) As String

Print "Before LukeSplit:"
Print "  ReDim MyArray(-1) As String:"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
Print "  MyString       =" + Chr$(34) + MyString + Chr$(34)
Print "  MyDelim        =" + Chr$(34) + MyDelim + Chr$(34)
Print

LukeSplit MyString, ",", MyArray()
Print "After LukeSplit MyString, " + Chr$(34) + "," + Chr$(34) + ", MyArray():"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
For iLoop = LBound(MyArray) To UBound(MyArray)
    Print "  MyArray(" + _Trim$(Str$(iLoop)) + ")=" + Chr$(34) + MyArray(iLoop) + Chr$(34)
Next iLoop
Print

Print "Press any key to continue.": Sleep: _KeyClear: '_Delay 1

' =============================================================================
Cls
Print "Compare array split functions, test #2-A"
Print

MyString = "a,b,c,d,e,f"
MyDelim = ","
ReDim MyArray(-1) As String

Print "Before BplusSplit:"
Print "  ReDim MyArray(-1) As String:"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
Print "  MyString       =" + Chr$(34) + MyString + Chr$(34)
Print "  MyDelim        =" + Chr$(34) + MyDelim + Chr$(34)
Print

BplusSplit MyString, ",", MyArray()
Print "After BplusSplit MyString, " + Chr$(34) + "," + Chr$(34) + ", MyArray():"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
For iLoop = LBound(MyArray) To UBound(MyArray)
    Print "  MyArray(" + _Trim$(Str$(iLoop)) + ")=" + Chr$(34) + MyArray(iLoop) + Chr$(34)
Next iLoop
Print

Print "Press any key to continue.": Sleep: _KeyClear: '_Delay 1

' =============================================================================
Cls
Print "Compare array split functions, test #2-B"
Print

MyString = "g,h,i,j,k,l"
MyDelim = ","
ReDim MyArray(10 To 12) As String

Print "Before BplusSplit:"
Print "  ReDim MyArray(-1) As String:"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
Print "  MyString       =" + Chr$(34) + MyString + Chr$(34)
Print "  MyDelim        =" + Chr$(34) + MyDelim + Chr$(34)
Print

BplusSplit MyString, ",", MyArray()
Print "After BplusSplit MyString, " + Chr$(34) + "," + Chr$(34) + ", MyArray():"
Print "  lbound(MyArray)=" + _Trim$(Str$(LBound(MyArray)))
Print "  ubound(MyArray)=" + _Trim$(Str$(UBound(MyArray)))
For iLoop = LBound(MyArray) To UBound(MyArray)
    Print "  MyArray(" + _Trim$(Str$(iLoop)) + ")=" + Chr$(34) + MyArray(iLoop) + Chr$(34)
Next iLoop
Print

Print "Press any key to continue.": Sleep: _KeyClear: '_Delay 1

' /////////////////////////////////////////////////////////////////////////////

' FROM:
' An hash array dictonary step by step (reply #14)
' https://qb64phoenix.com/forum/showthread.php?tid=1547&pid=14929#pid14929

' note: I buggered this twice now,
' FOR base 1 array REDIM MyArray (1 to 1) AS ...
' the (1 to 1) is not same as (1) which was the Blunder!!!

' notes: REDIM the array(0) to be loaded before calling Split
' <<<< IMPORTANT dynamic array and empty, can use any lbound though

' This SUB will take a given N delimited string, and delimiter$
' and create an array of N+1 strings using the LBOUND of the given dynamic array to load.

' notes: the loadMeArray() needs to be dynamic string array
' and will not change the LBOUND of the array it is given.
' rev 2019-08-27

Sub BplusSplit (SplitMeString As String, delim As String, loadMeArray() As String)
    Dim curpos As Long
    Dim arrpos As Long
    Dim LD As Long
    Dim dpos As Long
    curpos = 1
    arrpos = LBound(loadMeArray) ' fix use the Lbound the array already has
    LD = Len(delim)
    dpos = InStr(curpos, SplitMeString, delim)
    Do Until dpos = 0
        loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
        arrpos = arrpos + 1
        If arrpos > UBound(loadMeArray) Then
            ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
        End If
        curpos = dpos + LD
        dpos = InStr(curpos, SplitMeString, delim)
    Loop
    loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
    ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String ' get the ubound correct
End Sub ' BplusSplit

' /////////////////////////////////////////////////////////////////////////////
' Splits a string in$ by delimeter delimiter$
' into an array result$().

' Split and join strings
' https://www.qb64.org/forum/index.php?topic=1073.0
'
' FROM luke, QB64 Developer
' Date: February 15, 2019, 04:11:07 AM
'
' Given a string of words separated by spaces (or any other character),
' splits it into an array of the words. I've no doubt many people have
' written a version of this over the years and no doubt there's a million
' ways to do it, but I thought I'd put mine here so we have at least one
' version. There's also a join function that does the opposite
' array -> single string.
'
' Code is hopefully reasonably self explanatory with comments and a little demo.
' Note, this is akin to Python/JavaScript split/join, PHP explode/implode.

' Split in$ into pieces, chopping at every occurrence of delimiter$.
' Multiple consecutive occurrences of delimiter$ are treated as a single instance.
' The chopped pieces are stored in result$().

' delimiter$ must be one character long.
' result$() must have been REDIMmed previously.

' Modified to handle multi-character delimiters

Sub LukeSplit (in$, delimiter$, result$())
    Dim start As Integer
    Dim finish As Integer
    Dim iDelimLen As Integer
    ReDim result$(-1)

    iDelimLen = Len(delimiter$)

    start = 1
    Do
        While Mid$(in$, start, iDelimLen) = delimiter$
            start = start + iDelimLen
            If start > Len(in$) Then
                Exit Sub
            End If
        Wend
        finish = InStr(start, in$, delimiter$)
        If finish = 0 Then
            finish = Len(in$) + 1
        End If

        ReDim _Preserve result$(0 To UBound(result$) + 1)

        result$(UBound(result$)) = Mid$(in$, start, finish - start)
        start = finish + 1
    Loop While start <= Len(in$)
End Sub ' LukeSplit
Reply


Messages In This Thread
RE: An hash array dictonary step by step - by madscijr - 04-11-2023, 08:12 PM



Users browsing this thread: 6 Guest(s)