tomjpsir Posted November 3, 2013 Share Posted November 3, 2013 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 ??? Regret My English is bad.. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 3, 2013 Moderators Share Posted November 3, 2013 tomjpsir,Welcome to the AutoIt forum. I would do it like this:expandcollapse popup#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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
tomjpsir Posted November 3, 2013 Author Share Posted November 3, 2013 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 More sharing options...
Moderators Melba23 Posted November 3, 2013 Moderators Share Posted November 3, 2013 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:expandcollapse popup#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..... 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Gianni Posted November 3, 2013 Share Posted November 3, 2013 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 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
tomjpsir Posted November 4, 2013 Author Share Posted November 4, 2013 PincoPanco & Melba23 ,thank you very much ! it's great work ! I'm newbie autoit, like Autoit,love Autoit, thanks member's forum >>>yeah Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now