Jump to content

Help whit array - (Moved)


Recommended Posts

Hello friends, I come back to you for your help, I have a table which I need to add the repeated data, but only if they coincide in two columns, example:

If position [X] [0] and position [X] [1] have more matches in the array, they are added to position [X] [3].

I have tried to do it in the following way, it makes the first positions, but the others do not, I know what the error is, that's why I need your help, I can't think of anything else, thanks.

 

Func _Limpiar($bArray)
   Local $aFinal[1][3]
   $aFinal[0][0] = $bArray[0][0]
   $aFinal[0][1] = $bArray[0][1]
   $aFinal[0][2] = $bArray[0][2]
   _ArrayDelete($bArray,0)
   ;_ArrayDisplay($bArray)
   ;_ArrayDisplay($aFinal)

   While UBound($bArray,$UBOUND_ROWS) > 0
      Local $Index[0]

      For $i = 0 To UBound($bArray, $UBOUND_ROWS)-1

         If ($aFinal[UBound($aFinal, $UBOUND_ROWS)-1][0] == $bArray[$i][0]) Then
            _ArrayAdd($Index,$i)
         EndIf
      Next
      ;_ArrayDisplay($Index)
      ;_ArrayDisplay($bArray)

      If UBound($Index, $UBOUND_ROWS)>0 Then
         $num = UBound($aFinal, $UBOUND_ROWS)-1

         For $i=0 To UBound($Index, $UBOUND_ROWS)-1
            If ($aFinal[$num][0] == $bArray[$Index[$i]][0]) And ($aFinal[$num][1] == $bArray[$Index[$i]][1]) Then
               $aFinal[$num][2] += $bArray[$Index[$i]][2]
            Else
               Local $sFill = $bArray[$Index[$i]][0]&"|"&$bArray[$Index[$i]][1]&"|"&$bArray[$Index[$i]][2]
               _ArrayAdd($aFinal, $sFill)
            EndIf
         Next

         For $i=0 To UBound($Index, $UBOUND_ROWS)-1
            _ArrayDelete($bArray, 0)
         Next

      Else
         Local $sFill = $bArray[0][0]&"|"&$bArray[0][1]&"|"&$bArray[0][2]
         _ArrayAdd($aFinal, $sFill)
         _ArrayDelete($bArray, 0)
      EndIf
      ;_ArrayDisplay($bArray,"salida")
      ;_ArrayDisplay($aFinal,"prueba")

   WEnd

   Return $aFinal

EndFunc

image.png.45f26ff056dec42850c57db07e220028.png

 

Table example.

Edited by JuanFelipe
Link to comment
Share on other sites

2 hours ago, JuanFelipe said:

I have a table which I need to add the repeated data, but only if they coincide in two columns, example:

If position [X] [0] and position [X] [1] have more matches in the array, they are added to position [X] [3].

Could you explain some more as I cannot figure out  exactly what you want to achieve...

Link to comment
Share on other sites

Your data sample looks like some inventory data.  Do you mean that you have to merge (sum stock quantities) from identical {description, size} entries?

That is, for instance merge entries:
Codos | 4" | 6
Codos | 4" | 4
into:
Codos | 4" | 10

ans so on?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

On 11/28/2019 at 5:38 PM, jchd said:

Your data sample looks like some inventory data.  Do you mean that you have to merge (sum stock quantities) from identical {description, size} entries?

That is, for instance merge entries:
Codos | 4" | 6
Codos | 4" | 4
into:
Codos | 4" | 10

ans so on? 

Exactly.

Link to comment
Share on other sites

You can do like this:

#include "..\include\ArrayMultiColSort.au3"

; description, size, quantity
Local $aIn = [ _
    ["BBB", "4l", 1], _
    ["AAA", "8m", 10], _
    ["CCC", "1m²", 37], _
    ["BBB", "4l", 19], _
    ["EEE", "10kg", 7], _
    ["AAA", "12m", 17], _
    ["BBB", "4l", 4], _
    ["AAA", "8m", 11], _
    ["CCC", "1m²", 3], _
    ["BBB", "20l", 6], _
    ["AAA", "8m", 7], _
    ["DDD", '3"', 2] _
]

Local $bOut = _Limpiar($aIn)
_ArrayDisplay($bOut)

Func _Limpiar($b)
    Local $SortFmt = [[0, 0], [1, 0]]
    _ArrayMultiColSort($b, $SortFmt)
    Local $i = 0, $j = 0
    $b[$j][0] = $b[$i][0]
    $b[$j][1] = $b[$i][1]
    $b[$j][2] = $b[$i][2]
    For $i = 1 To UBound($b) - 1
        If $b[$i][0] = $b[$i - 1][0] And $b[$i][1] = $b[$i - 1][1] Then
            $b[$j][2] += $b[$i][2]
        Else
            $j += 1
            $b[$j][0] = $b[$i][0]
            $b[$j][1] = $b[$i][1]
            $b[$j][2] = $b[$i][2]
        EndIf
    Next
    ReDim $b[$j + 1][UBound($b, 2)]
    Return $b
EndFunc

using this UDF by @Melba23

 

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

10 hours ago, jchd said:

You can do like this:

#include "..\include\ArrayMultiColSort.au3"

; description, size, quantity
Local $aIn = [ _
    ["BBB", "4l", 1], _
    ["AAA", "8m", 10], _
    ["CCC", "1m²", 37], _
    ["BBB", "4l", 19], _
    ["EEE", "10kg", 7], _
    ["AAA", "12m", 17], _
    ["BBB", "4l", 4], _
    ["AAA", "8m", 11], _
    ["CCC", "1m²", 3], _
    ["BBB", "20l", 6], _
    ["AAA", "8m", 7], _
    ["DDD", '3"', 2] _
]

Local $bOut = _Limpiar($aIn)
_ArrayDisplay($bOut)

Func _Limpiar($b)
    Local $SortFmt = [[0, 0], [1, 0]]
    _ArrayMultiColSort($b, $SortFmt)
    Local $i = 0, $j = 0
    $b[$j][0] = $b[$i][0]
    $b[$j][1] = $b[$i][1]
    $b[$j][2] = $b[$i][2]
    For $i = 1 To UBound($b) - 1
        If $b[$i][0] = $b[$i - 1][0] And $b[$i][1] = $b[$i - 1][1] Then
            $b[$j][2] += $b[$i][2]
        Else
            $j += 1
            $b[$j][0] = $b[$i][0]
            $b[$j][1] = $b[$i][1]
            $b[$j][2] = $b[$i][2]
        EndIf
    Next
    ReDim $b[$j + 1][UBound($b, 2)]
    Return $b
EndFunc

using this UDF by @Melba23

 

 

THANK YOU, it works perfectly.

Link to comment
Share on other sites

Don't quote the full post you want to reply to, keep that for the part that need quoting, if any.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...