Jump to content

Help!Plz .. Find Duplicate String & Add Sum in File


 Share

Recommended Posts

  • Moderators

tomjpsir,

Welcome to the AutoIt forum. :)

I would do it like this:

#include <Constants.au3>

; Simulate reading in file to an array
$sLines = "A=1" & @CRLF & _
    "A=2" & @CRLF & _
    "A=3" & @CRLF & _
    "B=1" & @CRLF & _
    "B=2" & @CRLF & _
    "B=3" & @CRLF & _
    "C=3" & @CRLF & _
    "C=4" & @CRLF & _
    "C=5"
$aLines = StringSplit($sLines, @CRLF, 1)

; Declare counters
Global $iA = 0, $iB = 0, $iC = 0

; Now loop through file and add
For $i = 1 To $aLines[0]
    ; Read line
    $sLine = $aLines[$i]
    ; Split on the =
    $aSplit = StringSplit($sLine, "=")
    ; What have we on this line?
    Switch $aSplit[1]
        Case "A"
            ; Add value to appropriate counter
            $iA += $aSplit[2]
        Case "B"
            $iB += $aSplit[2]
        Case "C"
            $iC += $aSplit[2]
    EndSwitch
Next

; Now display results
MsgBox($MB_SYSTEMMODAL, "Result", "A = " & $iA & @CRLF & "B = " & $iB & @CRLF & "C = " & $iC)
Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

thank you Melba23

but my 'test.txt' has more than 100 lines..

=========

A=1

A=2

A=3

B=

B=

.

.

Y=

Z=

============

Use switch case function is too long to declare

can i use duplicate strings or stringsplit after add sum together

ex; stringpartern,stringinstr,filereadtoarray

thank again

Link to comment
Share on other sites

  • Moderators

tomjpsir,

In future, please give us the full requirement from the beginning as having the goal posts moved mid-thread is extremely annoying. It usually means that the assistance given previously is not adapted to the particular case and so a complete waste of time for those who bothered to answer - and who might not bother to continue. Point taken I hope. ;)

You now say that the "keys" are the letter A-Z - so we can use the ASCII value of these letters to index an array - like this:

#include <Array.au3>

; Simulate reading in file to an array
$sLines = "A=1" & @CRLF & _
    "A=2" & @CRLF & _
    "A=3" & @CRLF & _
    "B=1" & @CRLF & _
    "B=2" & @CRLF & _
    "B=3" & @CRLF & _
    "C=3" & @CRLF & _
    "C=4" & @CRLF & _
    "C=5" & @CRLF & _
    "X=1" & @CRLF & _
    "Y=2" & @CRLF & _
    "Z=3" & @CRLF & _
    "Z=4" & @CRLF & _
    "Z=5"

$aLines = StringSplit($sLines, @CRLF, 1)

; Declare counter array
Global $aCounters[27]

; Now loop through file and add
For $i = 1 To $aLines[0]
    ; Read line
    $sLine = $aLines[$i]
    ; Split on the =
    $aSplit = StringSplit($sLine, "=")
    ; What have we on this line?
    $iASCII = Asc(StringUpper($aSplit[1])) ; Get ASCII of letter
    $iIndex = $iASCII - 64 ; Convert to array index
    ; Add value to appropriate array element
    $aCounters[$iIndex] += $aSplit[2]
Next

; Now display results
_ArrayDisplay($aCounters)
Now you have an array with the totals for each letter in a separate element. So that should work for your "more than 100 lines" file. Unless you have still not told us everything..... :whistle:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

here another way that will work with any number of letters (lines in test.txt) also if the letters are longer than only one character

#include <File.au3>
#include <array.au3>

Local $test
; _FileReadToArray(".\test.txt", $test) ; read testtxt from disk file to array
;----------------------------------------------------------------------------------------------------------------
; Simulate reading in file to an array
; using the above code _FileReadToArray this reading simulation between this 2 lines must be deleted (of course)
Dim $test[18] = ["17", "A=1", "A=2", "A=3", "B=1", "B=2", "B=3", "C=3", "C=4", "C=5", "longletter=92", "longletter=8", "x=1", "x=1", "Y=2", "Z=3", "Z=4", "Z=5"]
;----------------------------------------------------------------------------------------------------------------
_ArrayDisplay($test, "Data from file") ; show data before calculatio
; create an 2D array to store splitted data column1 = letter, column2 = value
Local $data[$test[0]][2] ; zero based
For $i = 0 To $test[0] - 1
    $aSplit = StringSplit($test[$i + 1], "=") ; split letter from value
    $data[$i][0] = $aSplit[1] ; here goes letter
    $data[$i][1] = $aSplit[2] ; here goes value
Next
$letters = _ArrayUnique($data) ; array $letters contains one letter for type
Local $output[$letters[0]] ; create an array to store results
For $i = 1 To $letters[0] ; scan all letters present in file
    $letter = _ArrayFindAll($data, $letters[$i]) ; find all records for each letter type
    For $ii = 0 To UBound($letter) - 1
        $output[$i - 1] += $data[$letter[$ii]][1] ; sum all values of the same letter
    Next ; next value
    $output[$i - 1] = $letters[$i] & "=" & $output[$i - 1] ; format output
Next ; next letter
_ArrayDisplay($output) ; show result after calculation

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use 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...