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

## Recommended Posts

I have a  "test.txt" file as:

==========

ex:

A=1

A=2

A=3

B=1

B=2

B=3

C=3

C=4

C=5

========

How do converted into "newtest.txt " as

=====

A=6

B=6

C=12

====

Help me plz..Which' Function can use ???

##### Share on other sites

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]
\$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)```

M23

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

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

thank again

##### Share on other sites

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]
\$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.....

M23

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

##### 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
;----------------------------------------------------------------------------------------------------------------
; 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```

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

PincoPanco & Melba23 ,thank you very much !

it's great work !

I'm newbie autoit, like Autoit,love Autoit, thanks member's forum >>>yeah

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...