Search code examples
arraysautoit

Compare two arrays of .ini file entries


I have one .ini file as below:

[Step]
A=DONE
B=DONE
C=DONE
D=DONE

I need to get the [Step] section and put it in an array . Below is what I do:

$iniSection_Step = "Step"
$PrevStep = ""
Local $Prev = IniReadSection($iniPath_LogFile, $iniSection_Step)

For $i = 1 To $Prev[0][0]
    $PrevStep = $PrevStep &"|"& $Prev[$i][0]
Next
Global $PrevArray = StringSplit($PrevStep,"|",1)

The _ArrayDisplay() result:

Row|Col 0
 [0]|5
 [1]|
 [2]|A
 [3]|B
 [4]|C
 [5]|D

Now I need to compare the array with another and if an element exists in both, it will increment one array.

For $j = 0 To UBound($array_StepComplete) - 1
    if StringInStr($array_StepComplete[$j],$PrevArray[$i]) Then
        GUICtrlSetData($Input_PresentStep,$array_StepComplete[$j+1])
    EndIf
Next

This will increment one array but if someone deletes the content of .ini file as below:

  [Step]
  A=DONE

  C=DONE
  D=DONE

The code will increment one array but it does not check if the element exists.


Solution

  • What I understood: Compare two arrays and do some action if two elements match.

    First you need to clear things up. What do you want to compare? The order obvious does matter .. so I decided to Loop on the dynamical array first and if I found a matching pair I also exiting the loop for resource reasons ...

    Local $aSteps = IniReadSection("Steps.ini", "Step")
    If @error Then
        ConsoleWrite("#1 An error occured while reading the 'Steps.ini' file." & @CRLF)
        Exit
    EndIf
    
    Local $aAllsteps = IniReadSection("Steps.ini", "Allsteps")
    If @error Then
        ConsoleWrite("#2 An error occured while reading the 'Steps.ini' file." & @CRLF)
        Exit
    EndIf
    
    ; loop on the dynamical array first
    For $i = 1 To $aSteps[0][0]
        ; then loop on the static array for each element
        For $y = 1 To $aAllsteps[0][0]
            ; check if the elements match
            If $aSteps[$i][0] = $aAllsteps[$y][0] Then
                ; if the element match print out and exit loop for resource reason
                ConsoleWrite("MATCH - " & $aSteps[$i][0] & @CRLF)
                ExitLoop
            EndIf
        Next
    Next
    

    Steps.ini

    [Step]
    A=DONE
    C=DONE
    D=DONE
    [Allsteps]
    A=DONE
    B=DONE
    C=DONE
    D=DONE
    

    Output

    MATCH - A
    MATCH - C
    MATCH - D