Jump to content

Need help separating duplicate values


Recommended Posts

I am doing a post product automation, 

I have

Size:1|Name1
Size:2|Name2
Size:4|Name3
Size:1|FirstName1
Size:2|FirstName2
Size:3|FirstName3

How can i split them into 2 groups 

Group 1
Size:1|Name1
Size:2|Name2
Size:4|Name3

Group 2

Size:1|FirstName1
Size:2|FirstName2
Size:3|FirstName3

I tried many ways but it did not work

Because of duplicate Size: 1 and Size: 2, I can not post

 

Edited by JonnyQuy
Link to comment
Share on other sites

I would love to help but my RegEx skillz positively stink. However, I bet that the guru's will spot this and have an answer at some point soon. This sure looks like a regular expression search through an array to me.

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

$Check = 'Check.txt'
Local $Tatcasku[0][2]
Local $Soluong[0][1]



;~ #cs
For $i = 1 To _FileCountLines($Check)
    $Line = FileReadLine($Check, $i)

    $Size = StringSplit($Line, "|")
    $AllTT = $Size[1] & "|" & $Size[2]
    $SL = $Size[1]
    _ArrayAdd($Tatcasku, $AllTT)
    _ArrayAdd($Soluong, $SL)
Next
$Soluong = _ArrayUnique($Soluong)
_ArrayDelete($Soluong, 0)
_ArrayDisplay($Tatcasku)
_ArrayDisplay($Soluong)
Local $KiemTraSKU[UBound($Soluong)]



Local $SoluongSKu[UBound($Soluong)]
For $i = 0 To UBound($Soluong) - 1
    For $j = 0 To UBound($Tatcasku) - 1
        If StringInStr($Soluong[$i], $Tatcasku[$j][0]) = True Then
            $SoluongSKu[$i] = $SoluongSKu[$i] + 1
        EndIf
    Next
Next
;~ #CE
_ArrayDisplay($SoluongSKu)

 

Link to comment
Share on other sites

Local $Array[0][2]
For $i = 1 To _FileCountLines($Check)
    $Line = FileReadLine($Check, $i)

    _ArrayAdd($Array, $Line)
Next
_ArrayDisplay($Array)
$Arrayad = _ArrayUnique($Array)
_ArrayDelete($Arrayad, 0)
_ArrayDisplay($Arrayad)
Local $Soluong[UBound($Arrayad)]

For $i = 0 To UBound($Arrayad) - 1
    For $j = 1 To _FileCountLines($Check)
        $Line = FileReadLine($Check, $j)
        If StringInStr($Arrayad[$i], $Line) = True Then
            $Soluong[$i] = $Soluong[$i] + 1
        EndIf
    Next
Next

$Max = $Soluong[0]
For $i = 0 To UBound($Soluong) - 1
    If $Max < $Soluong[$i] Then
        $Max = $Soluong[$i]
    EndIf
Next
ConsoleWrite($Max & @CR)

;sử dụng vòng lặp DO While Sử dụng biện pháp trừ dần nếu Ubound $Array = 0 thì thoát vòng lặp
Local $Size = ''
For $i = 0 To $Max - 1

    ;nếu $Arrayad = $Array thì chạy còn khác thì bỏ qua

    $Size &= ' ' & $Array[$i][0]

Next
MsgBox(0, 0, $Size)

 

Link to comment
Share on other sites

I think you posted the whole thing in 2 parts, correct? I got the following to compile. Not tested yet.

#include <File.au3>

$Check = 'Check.txt'
Local $Tatcasku[0][2]
Local $Soluong[0][1]

;~ #cs
For $i = 1 To _FileCountLines($Check)
    $Line = FileReadLine($Check, $i)

    $Size = StringSplit($Line, "|")
    $AllTT = $Size[1] & "|" & $Size[2]
    $SL = $Size[1]
    _ArrayAdd($Tatcasku, $AllTT)
    _ArrayAdd($Soluong, $SL)
Next
$Soluong = _ArrayUnique($Soluong)
_ArrayDelete($Soluong, 0)
_ArrayDisplay($Tatcasku)
_ArrayDisplay($Soluong)
Local $KiemTraSKU[UBound($Soluong)]



Local $SoluongSKu[UBound($Soluong)]
For $i = 0 To UBound($Soluong) - 1
    For $j = 0 To UBound($Tatcasku) - 1
        If StringInStr($Soluong[$i], $Tatcasku[$j][0]) = True Then
            $SoluongSKu[$i] = $SoluongSKu[$i] + 1
        EndIf
    Next
Next
;~ #CE
_ArrayDisplay($SoluongSKu)

Local $Array[0][2]


For $i = 1 To _FileCountLines($Check)
    $Line = FileReadLine($Check, $i)

    _ArrayAdd($Array, $Line)
Next
_ArrayDisplay($Array)
$Arrayad = _ArrayUnique($Array)
_ArrayDelete($Arrayad, 0)
_ArrayDisplay($Arrayad)
Local $Soluong[UBound($Arrayad)]

For $i = 0 To UBound($Arrayad) - 1
    For $j = 1 To _FileCountLines($Check)
        $Line = FileReadLine($Check, $j)
        If StringInStr($Arrayad[$i], $Line) = True Then
            $Soluong[$i] = $Soluong[$i] + 1
        EndIf
    Next
Next

$Max = $Soluong[0]
For $i = 0 To UBound($Soluong) - 1
    If $Max < $Soluong[$i] Then
        $Max = $Soluong[$i]
    EndIf
Next
ConsoleWrite($Max & @CR)

;s? d?ng vòng l?p DO While S? d?ng bi?n pháp tr? d?n n?u Ubound $Array = 0 thì thoát vòng l?p
Local $Size = ''
For $i = 0 To $Max - 1

    ;n?u $Arrayad = $Array thì ch?y còn khác thì b? qua

    $Size &= ' ' & $Array[$i][0]

Next
MsgBox(0, 0, $Size)

 

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

16 minutes ago, Earthshine said:

ok. I will be back in office tomorrow to look further at it. I bet some hot shot here answers it faster/better though.

Local $TachSP[0][1]

Local $SoluongSKu[UBound($Soluong)]
Local $size = ''
For $i = 0 To UBound($Soluong) - 1
    For $j = 1 To UBound($Soluong) - 1
        If StringInStr($Tatcasku[0][0] , $Tatcasku[$j][0] ) = True Then ExitLoop
        $size &= ' ' & $Tatcasku[$j][0]
    Next

    ExitLoop
Next
$size = $Tatcasku[0][0] & $size
    MsgBox(0, 0, $size & @CR)

I can only do this

Link to comment
Share on other sites

Here are some regular expression methods for manipulating strings.

#include <Array.au3>

Local $Check = 'Check.txt' ; Post#1 of https://www.autoitscript.com/forum/topic/193926-need-help-separating-duplicate-values/

$aG1 = _ArrayReadFoundLine($Check, "\bName\d")      ; Find a line with "Name" between word boundary, "\b",and a digit, "\d".
_ArrayDisplay($aG1, "$aG1")
;Or
$aG2 = _ArrayReadFoundLine($Check, "\|\w{1,4}\d")   ; Find a line with 1 to 4 word characters, "\w", between a literal, (an escaped) "|" character, and a digit,"\d".
_ArrayDisplay($aG2, "$aG2")

$aG3 = _ArrayReadFoundLine($Check, "\bFirstName\d") ; Find a line with "FirstName" between word boundary, "\b", and a digit, "\d".
_ArrayDisplay($aG3, "$aG3")
; Or
$aG4 = _ArrayReadFoundLine($Check, "\|\w{5,}\d")    ; Find a line with 5 or more word characters, "\w", between a literal, (an escaped) "|" character, and a digit,"\d".
_ArrayDisplay($aG4, "$aG4")

; This part is added only for interest. 
ConsoleWrite(_ArrayToString($aG4, @CRLF) & @CRLF)
$aG4_2D = _StringTo2DArray(_ArrayToString($aG4, @CRLF), "|")
_ArrayDisplay($aG4_2D, "$aG4_2D")


; Note: for info on "\w", "{x,y}", "\d", "\b", or "(?i)", see StringRegExp() function in AutoIt help.
Func _ArrayReadFoundLine($sFlleName, $sREPattern)
    Return StringRegExp(FileRead($sFlleName), "(?i).*" & $sREPattern & ".*", 3) ; ".*" - Capture all characters (if any exist) before and after any captured character with $sREPattern on the same line.
EndFunc   ;==>_ArrayReadFoundLine

; Or, see _FileReadToArray() function in AutoIt help.
Func _StringTo2DArray($sString, $sDelim_Columns = ",", $sDelim_Row = @CRLF)
    ; ---- Find number of columns ($iCols) to correctly dimension array ----
    Local $iCols = 0, $sRE = "([^" & StringRegExpReplace($sDelim_Columns & $sDelim_Row, "([\[\]\-\^\\])", "\\$1") & "]+)"
    Local $lineArr = StringSplit(StringRegExpReplace($sString, $sRE, ""), $sDelim_Row, 1) ; 1 = entire delimiter string is needed to mark the split.
    ;_ArrayDisplay($lineArr)
    For $i = 1 To $lineArr[0]
        If (StringLen($lineArr[$i]) + 1) > $iCols Then $iCols = StringLen($lineArr[$i]) + 1
    Next
    ; ---- End of Find number of columns ----

    Local $aArray[0][$iCols]
    _ArrayAdd($aArray, $sString, 0, $sDelim_Columns, $sDelim_Row)
    Return $aArray
EndFunc   ;==>_StringTo2DArray

 

Link to comment
Share on other sites

Try this:

Local $sText = _
    "Size:1|Name1" & @CRLF & _
    "Size:2|Name2" & @CRLF & _
    "Size:4|Name3" & @CRLF & _
    "Size:1|FirstName1" & @CRLF & _
    "Size:2|FirstName2" & @CRLF & _
    "Size:3|FirstName3"

Local $aGrouped = StringRegExp($sText, "(?s)(([^|]+).+)(?=\r\n\2)\R(.+)", 3)
_ArrayDisplay($aGrouped)

First group is $aGroup[0], second group is $aGroup[1]

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

:)

Local $sText = _
    "Size:1|Name1" & @CRLF & _
    "Size:2|Name2" & @CRLF & _
    "Size:4|Name3" & @CRLF & _
    "Size:1|FirstName1" & @CRLF & _
    "Size:2|FirstName2" & @CRLF & _
    "Size:3|FirstName3"

Local $aGroup1 = StringRegExpReplace($sText, "(?s)([^|]+).*?\K\R\1.*", "")
Msgbox(0,"", $aGroup1)

Local $aGroup2 = StringReplace($sText, $aGroup1 & @crlf , "")
Msgbox(0,"", $aGroup2)

 

Edited by mikell
jc made me sad so I had to do it
Link to comment
Share on other sites

@mikell, omg, using two calls again ... and $aGroup1 has a parasitic trailing @CRLF :evil:

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