Jump to content

Help/example - combining the data in my 2d array where items in one column are duplicated?


Recommended Posts

So I am parsing some log files and I grab from them a date and a byte count. But - on some days, there is more than one entry, example in the code below on 3/11/2020 there are two entries. I need to combine the byte counts for the days that have duplicates (and output the byte counts in GB, but that is the easy part).

It took me a while to get this sorted but the below appears to work OK, yet I can't help but think there is a better/cleaner way to do it.

Anyone have ideas?

#include <array.au3>

Local $a_inArr[7][2] = [["2020-03-11", "122589268273"], ["2020-03-12", "6901211441"], ["2020-03-13", "6934371045"], ["2020-03-11", "6428227853"], ["2020-03-16", "7358355171"], ["2020-03-17", "7113079607"], ["2020-03-18", "6907515243"]]
Local $a_outArr[0][2]

$a_fndDup = _ArrayUnique($a_inArr)

For $i = 1 To UBound($a_fndDup) - 1
    $a_fndArr = _ArrayFindAll($a_inArr, $a_fndDup[$i])
    If IsArray($a_fndArr) And UBound($a_fndArr) >= 2 Then
        Local $s_totDta = 0
        For $j = 0 To UBound($a_fndArr) - 1
            $s_date = $a_inArr[$i][0]
            $s_totDta = $s_totDta + $a_inArr[$a_fndArr[$j]][1]
        Next
        _ArrayAdd($a_outArr, $a_fndDup[$i] & "|" & Round($s_totDta / 1073741824, 2))
    EndIf
Next

For $i = 0 To UBound($a_outArr) - 1
    For $j = 1 To UBound($a_inArr) - 1
        If $a_inArr[$j][0] <> $a_outArr[$i][0] Then _ArrayAdd($a_outArr, $a_inArr[$j][0] & "|" & Round($a_inArr[$j][1] / 1073741824, 2))
    Next
Next
_ArraySort($a_outArr)

_ArrayDisplay($a_inArr, "Inbound Array", Default, Default, Default, "Date|Bytes")
_ArrayDisplay($a_outArr, "Outbound Array", Default, Default, Default, "Date|GB")

 

Always carry a towel.

Link to comment
Share on other sites

Found if I used the following your array (just duplicated the entire array from your post) it returned incorrect results:

Local $a_inArr[][2] = [["2020-03-11", "122589268273"], ["2020-03-12", "6901211441"], ["2020-03-13", "6934371045"], ["2020-03-11", "6428227853"], ["2020-03-16", "7358355171"], ["2020-03-17", "7113079607"], ["2020-03-18", "6907515243"], ["2020-03-11", "122589268273"], ["2020-03-12", "6901211441"], ["2020-03-13", "6934371045"], ["2020-03-11", "6428227853"], ["2020-03-16", "7358355171"], ["2020-03-17", "7113079607"], ["2020-03-18", "6907515243"]]

The following works for me:

#include <array.au3>

Local $a_inArr[][2] = [["2020-03-11", "122589268273"], ["2020-03-12", "6901211441"], ["2020-03-13", "6934371045"], ["2020-03-11", "6428227853"], ["2020-03-16", "7358355171"], ["2020-03-17", "7113079607"], ["2020-03-18", "6907515243"], ["2020-03-11", "122589268273"], ["2020-03-12", "6901211441"], ["2020-03-13", "6934371045"], ["2020-03-11", "6428227853"], ["2020-03-16", "7358355171"], ["2020-03-17", "7113079607"], ["2020-03-18", "6907515243"]]

#Region Array
    Local $a_outArr = _ArrayUnique($a_inArr)
    _ArrayColInsert($a_outArr, 1)
    For $i = 1 To UBound($a_outArr) - 1
        $a_fndArr = _ArrayFindAll($a_inArr, $a_outArr[$i][0])
        If IsArray($a_fndArr) And UBound($a_fndArr) >= 1 Then
            For $j = 0 To UBound($a_fndArr) - 1
                $a_outArr[$i][1] += $a_inArr[$a_fndArr[$j]][1]
            Next
            $a_outArr[$i][1] = Round($a_outArr[$i][1] / 1073741824, 2)
        EndIf
    Next
    _ArrayDisplay($a_inArr, "Inbound Array", Default, Default, Default, "Date|Bytes")
    _ArrayDisplay($a_outArr, "Outbound Array", Default, Default, Default, "Date|GB")
#EndRegion Array

Or Scripting Dictionary (there might be a better way to do this).

#Region Scripting Dictionary
Local $a_OutArr = _GetDiskInfoSD()
    If @error Then Exit MsgBox(4096, "Error", "No data returned")

_ArrayDisplay($a_InArr, "Raw Disk Info", Default, Default, Default, "Date|Bytes")
_ArrayDisplay($a_OutArr, "Updated Disk Info", Default, Default, Default, "Date|GB")

Func _GetDiskInfoSD()
    Local $oDictionary = ObjCreate("Scripting.Dictionary")
    For $i = 0 To UBound($a_InArr) - 1
        If $oDictionary.Exists($a_InArr[$i][0]) Then
            $oDictionary.Item($a_InArr[$i][0]) += $a_InArr[$i][1]
        Else
            $oDictionary.Add($a_InArr[$i][0], $a_InArr[$i][1])
        EndIf
    Next
    If Not IsObj($oDictionary) Then Return SetError(1, 0, "")
    Local $aKeyItem = $oDictionary.Keys
        _ArrayColInsert($aKeyItem, 1)
    For $i = 0 To UBound($aKeyItem) - 1
        $aKeyItem[$i][1] = Round($oDictionary.Item($aKeyItem[$i][0]) / 1073741824, 2)
    Next
    Return SetError(0, 0, $aKeyItem)
EndFunc
#EndRegion Scripting Dictionary

 

Link to comment
Share on other sites

Using Subz's test data, this method returns the same result as Subz's example.

#include <array.au3>

Local $a_inArr[][2] = [ _
        ["2020-03-11", "122589268273"], _
        ["2020-03-12", "6901211441"], _
        ["2020-03-13", "6934371045"], _
        ["2020-03-11", "6428227853"], _
        ["2020-03-16", "7358355171"], _
        ["2020-03-17", "7113079607"], _
        ["2020-03-18", "6907515243"], _
        ["2020-03-11", "122589268273"], _
        ["2020-03-12", "6901211441"], _
        ["2020-03-13", "6934371045"], _
        ["2020-03-11", "6428227853"], _
        ["2020-03-16", "7358355171"], _
        ["2020-03-17", "7113079607"], _
        ["2020-03-18", "6907515243"]]

_ArraySort($a_inArr)

For $i = UBound($a_inArr) - 1 To 1 Step -1
    If $a_inArr[$i][0] == $a_inArr[$i - 1][0] Then
        $a_inArr[$i - 1][1] = $a_inArr[$i - 1][1] + $a_inArr[$i][1]
        _ArrayDelete($a_inArr, $i)
    Else
        $a_inArr[$i][1] = Round(($a_inArr[$i][1] / 2 ^ 30), 2) ; 1 GB = 2^30 bytes
    EndIf
Next
$a_inArr[0][1] = Round(($a_inArr[0][1] / 2 ^ 30), 2)

;ConsoleWrite(_ArrayToString($a_inArr) & @CRLF)
_ArrayDisplay($a_inArr, "Outbound Array", Default, Default, Default, "Date|GB")

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...