dersiniar Posted April 28, 2023 Posted April 28, 2023 (edited) Lol just got it to work. Sorry i didnt know better title for my question. I have 2 files on my desktop data.txt and data1.txt data.txt have more then 500 lines in it. Examples Customer 4052 Sea 554 apple 987 domino 877 pear 007 stone 0.000008 Keys 0.002 data1.txt have same lines but values are constantly changing Customer 5052.56 Sea 558 apple 987 domino 877 pear 007 stone 0.000006 Keys 0.0021 what i need is function that opens those files, and compares that data and show me result in console that have highest change. By this example "customer" have highest change. Now id like to get console outbut "customer 5052.56 24.69%" Can anyone help me with this. I have tried some samples, i have tried openGPT, but so far i havent find a way to get this to work. Edited April 28, 2023 by dersiniar
Developers Jos Posted April 28, 2023 Developers Posted April 28, 2023 Moved to the appropriate AutoIt General Help and Support forum, as the AutoIt Example Scripts forum very clearly states: Quote Share your cool AutoIt scripts, UDFs and applications with others. Do not post general support questions here, instead use the AutoIt Help and Support forums. Moderation Team SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Dan_555 Posted April 28, 2023 Posted April 28, 2023 Maybe like this ? expandcollapse popup#include <FileConstants.au3> $quit = 0 Local $hFileOpen = FileOpen(@ScriptDir & "\data.txt", $FO_READ) If @error <> 0 Then ConsoleWrite("Error while opening data.txt" & @CRLF) Exit EndIf Local $hFileOpen1 = FileOpen(@ScriptDir & "\data1.txt", $FO_READ) If @error <> 0 Then ConsoleWrite("Error while opening data1.txt" & @CRLF) Exit EndIf $line = 0 $c1 = 0 $c2 = 0 $debug = 0 Do $line = $line + 1 $tmp = FileReadLine($hFileOpen, $line) $err = @error If $err = 0 Then $tmps = StringInStr($tmp, " ") $tmpl = StringMid($tmp, 1, $tmps - 1) $tmpr = Number(StringMid($tmp, $tmps + 1)) If $debug = 1 Then ConsoleWrite($tmpl & " / " & $tmpr & " ") If $line = 1 Then $info1 = $tmp $quit1 = 0 $line1 = 0 Do $line1 = $line1 + 1 $tmp1 = FileReadLine($hFileOpen1, $line1) $err1 = @error If $err1 = 0 Then $tmp1s = StringInStr($tmp1, " ") $tmp1l = StringMid($tmp1, 1, $tmp1s - 1) $tmp1r = Number(StringMid($tmp1, $tmp1s + 1)) If $line1 = 1 Then $info2 = $tmp1 If $tmpl = $tmp1l And $line > 1 And $line1 > 1 Then If $debug = 1 Then ConsoleWrite("/" & $tmp1r & @CRLF) If $tmpr > $tmp1r Then $c1 = $c1 + 1 If $tmpr < $tmp1r Then $c2 = $c2 + 1 EndIf Else $quit1 = 1 EndIf Until $quit1 = 1 Else $quit = 1 EndIf Until $quit = 1 FileClose($hFileOpen) FileClose($hFileOpen1) If $c1 > $c2 Then ConsoleWrite($info1 & " " & (($c1 / $line) * 100) & "%" & @CRLF) ElseIf $c1 < $c2 Then ConsoleWrite($info2 & " " & (($c2 / $line1) * 100) & "%" & @CRLF) Else ConsoleWrite($info1 & " = " & $info2 & @CRLF) EndIf Some of my script sourcecode
Nine Posted April 28, 2023 Posted April 28, 2023 (edited) Streamlined version : #include <File.au3> #include <Math.au3> Local $aData1, $aData2, $iMax = 0, $iIndex, $iRatio _FileReadToArray("data1.txt", $aData1, Default, " ") _FileReadToArray("data2.txt", $aData2, Default, " ") If $aData1[0][0] <> $aData2[0][0] Then Exit MsgBox($MB_OK, "", "Files not equal") For $i = 1 to $aData1[0][0] If $aData1[$i][0] <> $aData2[$i][0] Then Exit MsgBox($MB_OK, "", "Keys not equal") $aData1[$i][1] = Number($aData1[$i][1]) $aData2[$i][1] = Number($aData2[$i][1]) $iRatio = Abs($aData1[$i][1] - $aData2[$i][1]) / _Min($aData1[$i][1], $aData2[$i][1]) If $iRatio > $iMax Then $iMax = $iRatio $iIndex = $i EndIf Next ConsoleWrite("Max difference is on " & $aData1[$iIndex][0] & " with ratio of " & Round($iMax * 100, 2) & "%" & @CRLF) BTW the highest ratio is on stone Edited April 28, 2023 by Nine mikell idea of using abs function “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
ioa747 Posted April 28, 2023 Posted April 28, 2023 here is my approach expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <Array.au3> Global $aData[1][4] $aData[0][0] = 0 ; "Value" $aData[0][1] = "data" $aData[0][2] = "data1" $aData[0][3] = "%" _dataToArray() _ArrayDisplay($aData, "$aData") ;---------------------------------------------------------------------------------------- Func _dataToArray() Local $aArray = FileReadToArray(@ScriptDir & "\data.txt") Local $iLineCount = @extended If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file data.txt @error: " & @error) ; An error occurred reading the file. Else For $i = 1 To $iLineCount Local $CurLine = $aArray[$i - 1] Local $Col = StringSplit($CurLine, " ") ReDim $aData[UBound($aData) + 1][4] $aData[$i][0] = $Col[1] ; Value $aData[$i][1] = $Col[2] ; Data Next $aData[0][0] = $i _data1ToArray() EndIf EndFunc ;==>_dataToArray ;---------------------------------------------------------------------------------------- Func _data1ToArray() Local $aArray = FileReadToArray(@ScriptDir & "\data1.txt") Local $iLineCount = @extended If @error Then MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file data1.txt. @error: " & @error) ; An error occurred reading the file. Else For $i = 1 To $iLineCount Local $CurLine = $aArray[$i - 1] Local $Col = StringSplit($CurLine, " ") Local $IDx = _ArraySearch($aData, $Col[1], 1, 0, 0, 0, 1, 0) $aData[$IDx][2] = $Col[2] ; Data1 $aData[$IDx][3] = StringFormat("%.2f%", (($aData[$IDx][2] - $aData[$IDx][1]) / $aData[$IDx][1]) * 100) Next Local $iMax = _ArrayMaxIndex($aData, 1, 1, -1, 3) ConsoleWrite($aData[$iMax][0] & " " & $aData[$iMax][2] & " " & $aData[$iMax][3] & @CRLF) EndIf EndFunc ;==>_data1ToArray ;---------------------------------------------------------------------------------------- I know that I know nothing
mikell Posted April 28, 2023 Posted April 28, 2023 For the fun #Include <Array.au3> Local $txt1 = FileRead("data.txt"), $txt2 = FileRead("data1.txt") $a1 = StringRegExp($txt1, '(?m)^\S+\s(.*)$', 3) $a2 = StringRegExp($txt2, '(?m)^\S+\s(.*)$', 3) $lines = UBound($a1) Local $diff[$lines] For $i = 0 to $lines - 1 $diff[$i] = Abs($a2[$i] - $a1[$i]) Next $max = _ArrayMax($diff) For $i = 0 to $lines-1 If $diff[$i] = $max Then $item = StringRegExp($txt1, '(?m)(?:^.*$\R){' & $i & '}(\S+)', 1)[0] $percent = Round(100*(($a2[$i]/$a1[$i]) - 1), 2) EndIf Next Msgbox(0,"", $item & @crlf & $percent & " %")
ioa747 Posted April 28, 2023 Posted April 28, 2023 (edited) @Nine i have this 1517410-compare-to-txt-files-and-find-highest-change-(Moved).au3" (9) : ==> Subscript used on non-accessible variable.: If $aData1[0][0] <> $aData2[0][0] Then Exit MsgBox($MB_OK, "", "Files not equal") If $aData1[0][0] <> $aData2^ ERROR Edit: OK I found it was "data1.txt", "data2.txt" instead of "data.txt", "data1.txt" Edited April 28, 2023 by ioa747 I know that I know nothing
ioa747 Posted April 28, 2023 Posted April 28, 2023 if we make "data.txt" into "data1.txt", and "data1.txt" into "data.txt", what should it show us? I know that I know nothing
Nine Posted April 28, 2023 Posted April 28, 2023 (edited) @mikell It should be stone as the difference is larger (in comparaison). It gives 33.3%. Ok ill explain. Remove all dot and zeros (which are identical), so you have 6 and 8. The difference is 2. 2 over the lowest which is 6, gives 2 / 6 = 33.3%. Basic math BTW I liked your regexp attempt, just redo it to make it work ps. good idea to use abs, making the change Edited April 28, 2023 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
mikell Posted April 29, 2023 Posted April 29, 2023 (edited) 11 hours ago, Nine said: It should be stone as the difference is larger (in comparaison). It gives 33.3% Not really. As the 2nd value is lower than the first one it should be -33.3% (basic math ) OTOH the OP said 18 hours ago, dersiniar said: By this example "customer" have highest change and while I thought first about doing the same way than yours, it's the reason why I did it differently So depending on what the OP really wants one among both options in my silly snippet below must be (un)commented #Include <Array.au3> Local $txt1 = FileRead("data.txt"), $txt2 = FileRead("data1.txt") $a1 = StringRegExp($txt1, '\S+\s(.*)', 3) $a2 = StringRegExp($txt2, '\S+\s(.*)', 3) $lines = UBound($a1) Local $diff[$lines] For $i = 0 to $lines - 1 $diff[$i] = Round(100*((($a2[$i]>$a1[$i] ? $a2[$i] : $a1[$i])/($a2[$i]<$a1[$i] ? $a2[$i] : $a1[$i])) - 1), 2) ; If $a2[$i]<$a1[$i] Then $diff[$i] = $diff[$i]*-1 ; <== option 1 Next ;_ArrayDisplay($diff) $max = _ArrayMax($diff) For $i = 0 to $lines-1 If $diff[$i] = $max Then $item = StringRegExp($txt1, '(?:.*\R){' & $i & '}(\S+)', 1)[0] If $a2[$i]<$a1[$i] Then $max = $max*-1 ; <== option 2 EndIf Next Msgbox(0,"", $item & @crlf & $max & " %") Edited April 29, 2023 by mikell regex simplification
Gianni Posted April 29, 2023 Posted April 29, 2023 (edited) Some observations on above code snippets that might cause problems: if the data names contain spaces if there are multiple values with the same delta (only one is shown). if there are increments where the previous data was 0 I'm not sure if the proposed delta% are correct. Shouldn't they be calculated as ((FinalData - InitialData)/ InitialData ) x 100 ? It's not clear if OP wants to find the largest numerical change or the largest percentage change? they do not necessarily overlap Here's another snippet to generate a table summarizing the calculated values, where you can see the differences between absolute deltas and percentage deltas. The maximum values detected are shown on the title of the window I haven't implemented the ConsoleWrite part, array should be also enumerated to see if there are multiple deltas with the same values. I hope I didn't write inaccuracies, sorry if I did ..... #include <Array.au3> Local $aData1 = FileReadToArray("data.txt"), $aData2 = FileReadToArray("data1.txt") Local $aOut[@extended + 1][5] = [['Name', 'Value pre', 'Value post', 'Delta Abs', 'Delta %']], $MaxDeltaPerc, $MaxDeltaAbs, $iNdx For $i = 0 To UBound($aData1) - 1 $iNdx += 1 $aOut[$iNdx][0] = StringLeft($aData1[$i], StringInStr($aData1[$i], " ", 0, -1)) $aOut[$iNdx][1] = StringTrimLeft($aData1[$i], StringInStr($aData1[$i], " ", 0, -1)) $aOut[$iNdx][2] = StringTrimLeft($aData2[$i], StringInStr($aData2[$i], " ", 0, -1)) $aOut[$iNdx][3] = ($aOut[$iNdx][2] - $aOut[$iNdx][1]) $aOut[$iNdx][4] = $aOut[$iNdx][1] = 0 ? ChrW(8734) : ($aOut[$iNdx][3] / $aOut[$iNdx][1]) * 100 If $MaxDeltaAbs < Abs($aOut[$iNdx][3]) Then $MaxDeltaAbs = Abs($aOut[$iNdx][3]) If $MaxDeltaPerc < Abs($aOut[$iNdx][4]) Then $MaxDeltaPerc = Abs($aOut[$iNdx][4]) Next _ArrayDisplay($aOut, "Max " & ChrW(916) & " Abs = " & $MaxDeltaAbs & " ; Max " & ChrW(916) & " % " & $MaxDeltaPerc) Edited April 29, 2023 by Gianni Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Skeletor Posted May 2, 2023 Posted May 2, 2023 Firstly I do not know if my percentage calculations are what the OP is looking for: Finding the difference [ 5052.56 - 4052 = 1000.56 ] Then work out the percentage [ (1000.56 / 5052.56) * 100% = 19.81% ] Code below: #include <FileConstants.au3> Comparison() Func Comparison() Local $dFile = @DesktopDir & "\data.txt" Local $d1File = @DesktopDir & "\data1.txt" Local $data = FileRead($dFile) Local $data1 = FileRead($d1File) Local $dArr = StringSplit($data, @CRLF, 1) Local $d1Arr = StringSplit($data1, @CRLF, 1) Local $maxDiff = 0 Local $maxLine = "" For $i = 1 To $dArr[0] Local $l = $dArr[$i] Local $v = StringRegExpReplace($l, "[^\d\.]", "") Local $l1 = $d1Arr[$i] Local $v1 = StringRegExpReplace($l1, "[^\d\.]", "") Local $diff = Abs($v1 - $v) If $diff > $maxDiff Then $maxDiff = $diff $maxLine = $l1 EndIf Next Local $percentChange = Round(($maxDiff / StringRegExpReplace($maxLine, "[^\d\.]", "")) * 100, 2) ConsoleWrite($maxLine & " has a difference of " & $percentChange & "%" & @CRLF) MsgBOx(-1,"",$maxLine & " has a difference of " & $percentChange & "%") EndFunc Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
ioa747 Posted May 2, 2023 Posted May 2, 2023 1 hour ago, Skeletor said: Then work out the percentage [ (1000.56 / 5052.56) * 100% = 19.81% ] In my opinion, (1000.56/5052.56) = 0.198 would work if you started with 5052.56, and wanted to see what reduction you got. Now that you have an increase, since your starting number is 4052, you should do (1000.56/4052) = 0.246 forgive me if I'm wrong thanks I know that I know nothing
Skeletor Posted May 2, 2023 Posted May 2, 2023 Makes sense, so then I would have to do it like so. Output to Console. Output is similar to that of @ioa747 #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 #include <FileConstants.au3> #include <Array.au3> Local $dFile = @DesktopDir & "\data.txt" Local $d1File = @DesktopDir & "\data1.txt" Local $d = FileReadToArray($dFile) If @error Then MsgBox(0, "", "Error reading " & $dFile) Exit EndIf Local $d1 = FileReadToArray($d1File) If @error Then MsgBox(0, "", "Error reading " & $d1File) Exit EndIf For $i = 0 To UBound($d) - 1 Local $row = StringSplit($d[$i], " ") Local $row1 = StringSplit($d1[$i], " ") Local $perc = (($row1[2] - $row[2]) / $row[2]) * 100 ConsoleWrite($row[1] & " " & $row1[2] & " " & Round($perc, 2) & "%" & @CRLF) Next Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
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