major_lee Posted May 13, 2021 Share Posted May 13, 2021 (edited) $splits = StringSplit($sFileRead, '=') If ($splits[0] > 1) Then If (IsNumber($splits[2]) And $splits[2] <= 10) Then ConsoleWrite($splits[1] & "::" & $splits[2] & @CRLF) EndIf EndIf I'm not sure if its just me or what is going on but its not working how i expected??? I am just cleaning up an ini file. If the value is greater then 10 then we want to delete that line. Also has to bypass keys or values that are not numbers. I'm also open to more effective approaches to accomplishing the task. Such as for loop _FileCountLines but that didn't work right for me. so i did if error flag exitloop which works well. Full code in quotes. Quote Full code #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <File.au3> ConsoleWrite("BOOT" & @CRLF&@CRLF) Example() Func Example() Local Const $sFilePath = (@ScriptDir & "\data.ini") ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then ConsoleWrite("An error occurred when reading the file." & @CRLF) Return False Else ; Read the fist line of the file using the handle returned by FileOpen. Local $sFileRead = FileReadLine($hFileOpen) For $i = 1 To 1000000 $sFileRead = FileReadLine($hFileOpen, $i) If (@error) Then ExitLoop Else $splits = StringSplit($sFileRead, '=') If ($splits[0] > 1) Then If (IsNumber($splits[2]) And $splits[2] <= 10) Then ConsoleWrite($splits[1] & "::" & $splits[2] & @CRLF) EndIf EndIf EndIf Next EndIf ; Close the handle returned by FileOpen. FileClose($hFileOpen) EndFunc ;==>Example … I read If the variable is a numeric value represented as a string, then IsNumber() will return 0. I suspect I would have to chr cycle to verify a value. then push it to a int()? Edited May 13, 2021 by major_lee ... Link to comment Share on other sites More sharing options...
major_lee Posted May 13, 2021 Author Share Posted May 13, 2021 goes to show what a little reading gets you.. but still , maybe there is a better way? Quote Working Code $splits = StringSplit($sFileRead, '=') If ($splits[0] > 1) Then If (IsNumber(Number($splits[2])) And Number($splits[2]) <= 10 And Number($splits[2]) <> 0) Then ConsoleWrite($splits[1] & "::" & $splits[2] & @CRLF) EndIf EndIf Link to comment Share on other sites More sharing options...
argumentum Posted May 13, 2021 Share Posted May 13, 2021 (edited) Func IsComfortablyNumber($string) ; reference https://www.youtube.com/watch?v=_FrOQC-zEog If Number($string) == Int($string) And Number($string) == $string Then Return SetError(0, 2, Int($string)) ; is Integer If Number($string) == $string Then Return SetError(0, 1, Number($string)) ; is Real Return SetError(0, 0, $string) EndFunc I have not seen the file you read, so, would this work ?. Edited May 13, 2021 by argumentum better code major_lee 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
mikell Posted May 13, 2021 Share Posted May 13, 2021 19 minutes ago, major_lee said: IsNumber(Number($splits[2])) Do you sometimes get this false ? BTW what about the Ini* functions ? Link to comment Share on other sites More sharing options...
major_lee Posted May 13, 2021 Author Share Posted May 13, 2021 1 minute ago, mikell said: Do you sometimes get this false ? BTW what about the Ini* functions ? The problem was, IsNumber($splits[2]) If (IsNumber($splits[2]) And $splits[2] <= 10) Then Documentation remarks says, If the variable is a numeric value represented as a string, then IsNumber() will return 0. Therefor the solution is. If (IsNumber(Number($splits[2])) And Number($splits[2]) <= 10 And Number($splits[2]) <> 0) Then Link to comment Share on other sites More sharing options...
mikell Posted May 13, 2021 Share Posted May 13, 2021 Try this msgbox(0,"", IsNumber(Number("test")) ) and this $var = "11" msgbox(0,"", $var > 10 ) Link to comment Share on other sites More sharing options...
argumentum Posted May 13, 2021 Share Posted May 13, 2021 (edited) ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("-1.5") & @TAB & @extended & @CRLF) ; Return @extended = yes/real ConsoleWrite(@ScriptLineNumber & @TAB & Number("-1.5") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("-1.5") & @CRLF & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("0") & @TAB & @extended & @CRLF) ; Return @extended = yes/integer ConsoleWrite(@ScriptLineNumber & @TAB & Number("0") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("0") & @CRLF & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("a") & @TAB & @extended & @CRLF) ; Return @extended = no ConsoleWrite(@ScriptLineNumber & @TAB & Number("a") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("a") & @CRLF & @CRLF) Func IsComfortablyNumber($string) ; reference https://www.youtube.com/watch?v=_FrOQC-zEog If Number($string) == Int($string) And Number($string) == $string Then Return SetError(0, 2, Int($string)) ; is Integer If Number($string) == $string Then Return SetError(0, 1, Number($string)) ; is Real Return SetError(0, 0, $string) EndFunc the OP is happy with the solution but, ...I believe that what I presented is more real. I mean, zero is a number Edited May 13, 2021 by argumentum better code FrancescoDiMuro and major_lee 2 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
major_lee Posted May 13, 2021 Author Share Posted May 13, 2021 (edited) Here is everything. Looks solid now. summary of data.ini Quote [info] counter=256 name=Dub [checks] 3508083115=1 3269532009=3 3183286609=33 3402111378=270 3353486253=2 3855754413=5 4094305009=4 3162121333=3 3164218273=5 3494781865=4 3712885941=3 4177404797=4 3537249249=3 3348441145=2 1578968073=2 [zone] 701826551= 761202179= 895682105= 3402111378= clearData.au3 expandcollapse popup#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <File.au3> ConsoleWrite("BOOT" & @CRLF&@CRLF) Example() Func Example() Local Const $sFilePath = (@ScriptDir & "\data.ini") Local Const $sFilePath1 = (@ScriptDir & "\temp.ini") ; Open the file for reading and store the handle to a variable. FileDelete($sFilePath1) Local $hFileOpen = FileOpen($sFilePath, $FO_READ ) Local $hFileOpen1 = FileOpen($sFilePath1, $FO_APPEND) If $hFileOpen = -1 Or $hFileOpen1 = -1 Then ConsoleWrite("An error occurred when reading the file." & @CRLF) Return False Else ; Read the fist line of the file using the handle returned by FileOpen. Local $sFileRead = FileReadLine($hFileOpen) For $i = 1 To _FileCountLines ( $sFilePath ) $sFileRead = FileReadLine($hFileOpen, $i) If (@error) Then ConsoleWrite($i&":"&"ExitLoop" & @CRLF) ExitLoop Else $splits = StringSplit($sFileRead, '=') If ($splits[0] <= 1) Then FileWriteLine($hFileOpen1, $sFileRead) ConsoleWrite($i&":"&$sFileRead & @CRLF) Else If (IsNumber(Number($splits[2])) And Number($splits[2]) <= 10 And Number($splits[2]) <> 0) Then ConsoleWrite($i&":wipe:"&$splits[1] & "=" & $splits[2] & @CRLF) Else FileWriteLine($hFileOpen1, $sFileRead) ConsoleWrite($i&":"&$splits[1] & "=" & $splits[2] & @CRLF) EndIf EndIf EndIf Next EndIf FileClose($hFileOpen) FileCopy($sFilePath1,$sFilePath, $FC_OVERWRITE ) EndFunc ;==>Example Edited May 13, 2021 by major_lee Link to comment Share on other sites More sharing options...
major_lee Posted May 13, 2021 Author Share Posted May 13, 2021 10 minutes ago, argumentum said: ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("1") & @TAB & @extended & @CRLF) ; Return @extended = yes ConsoleWrite(@ScriptLineNumber & @TAB & Number("1") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("1") & @CRLF & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("0") & @TAB & @extended & @CRLF) ; Return @extended = yes ConsoleWrite(@ScriptLineNumber & @TAB & Number("0") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("0") & @CRLF & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsComfortablyNumber("a") & @TAB & @extended & @CRLF) ; Return @extended = no ConsoleWrite(@ScriptLineNumber & @TAB & Number("a") & @CRLF) ConsoleWrite(@ScriptLineNumber & @TAB & IsNumber("a") & @CRLF & @CRLF) Func IsComfortablyNumber($string) ; reference https://www.youtube.com/watch?v=_FrOQC-zEog If Int($string) == $string Then Return SetError(0, 1, Int($string)) Return SetError(0, 0, $string) EndFunc the OP is happy with the solution but, ...I believe that what I presented is more real. I mean, zero is a number Yes, my issue was thinking about a string not knowing its a int. while the return was 0 still being a number. In this situation i can work around it. As understood 0 being a number and if the string was 0 in the first place then there would be a conflict. I think what would happen is splits[2] = 0 THEN IsNumber(Number($splits[2])) = 1 Link to comment Share on other sites More sharing options...
argumentum Posted May 13, 2021 Share Posted May 13, 2021 ConsoleWrite(IsNumber(Number("")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("1")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("0")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("a")) & @CRLF & @CRLF) ; = 1 It all return one Global $aArray[4] = ["0", "1", "a", ""] For $v = 0 To 3 ; it's only true with the "1", so it does what you need, hence, good =) ConsoleWrite( (IsNumber(Number($aArray[$v])) And Number($aArray[$v]) <= 10 And Number($aArray[$v]) <> 0) & @CRLF) Next Exit But you're looking for an int greater than zero, so it's all good == School time == Number: Returns the numeric representation of an expressionIsNumber: Checks if a variable's base type is numeric. So, Number returns a 0 or a 1, both integers. And you ask is IsNumber(), and that is what Number() returns, so, this logic goes nowhere fast. But then you ask: are you less than ten and not a zero ? and the answer is what you expected. What @mikell presented: Global $aArray[4] = ["0", "1", "a", ""] For $v = 0 To 3 ; it's only true with the "1", so it does what you need, hence, good =) ;~ ConsoleWrite( (IsNumber(Number($aArray[$v])) And Number($aArray[$v]) <= 10 And Number($aArray[$v]) <> 0) & @CRLF) ConsoleWrite( ( $aArray[$v] > 0 And $aArray[$v] <= 10 ) & @CRLF) ; @mikell Next is what you need. major_lee 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
major_lee Posted May 13, 2021 Author Share Posted May 13, 2021 4 minutes ago, argumentum said: ConsoleWrite(IsNumber(Number("")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("1")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("0")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("a")) & @CRLF & @CRLF) ; = 1 It all return one Global $aArray[4] = ["0", "1", "a", ""] For $v = 0 To 3 ; it's only true with the "1", so it does what you need, hence, good =) ConsoleWrite( (IsNumber(Number($aArray[$v])) And Number($aArray[$v]) <= 10 And Number($aArray[$v]) <> 0) & @CRLF) Next Exit But you're looking for an int greater than zero, so it's all good == School time == Number: Returns the numeric representation of an expressionIsNumber: Checks if a variable's base type is numeric. So, Number returns a 0 or a 1, both integers. And you ask is IsNumber(), and that is what Number() returns, so, this logic goes nowhere fast. But then you ask: are you less than ten and not a zero ? and the answer is what you expected. What @mikell presented: Global $aArray[4] = ["0", "1", "a", ""] For $v = 0 To 3 ; it's only true with the "1", so it does what you need, hence, good =) ;~ ConsoleWrite( (IsNumber(Number($aArray[$v])) And Number($aArray[$v]) <= 10 And Number($aArray[$v]) <> 0) & @CRLF) ConsoleWrite( ( $aArray[$v] > 0 And $aArray[$v] <= 10 ) & @CRLF) ; @mikell Next is what you need. I thought I was going to have to do this. just a rough draft. $string = "945" ConsoleWrite(@CRLF & "isStringNumber: " & $string & " - " & isStringNumber($string) & @CRLF) $string = "uys" ConsoleWrite(@CRLF & "isStringNumber: " & $string & " - " & isStringNumber($string) & @CRLF) Func isStringNumber($str) Local $iLength = StringLen($str) $return = False For $i = 1 To $iLength For $ii = 48 To 57 If Asc ( StringLeft($str, 1) ) == $ii Then $return = True ExitLoop Else $return = False EndIf Next $str = StringTrimLeft($str, 1) Next Return $return EndFunc ;==>isStringNumber Link to comment Share on other sites More sharing options...
mikell Posted May 13, 2021 Share Posted May 13, 2021 2 hours ago, argumentum said: ConsoleWrite(IsNumber(Number("")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("1")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("0")) & @CRLF) ; = 1 ConsoleWrite(IsNumber(Number("a")) & @CRLF & @CRLF) ; = 1 It all return one Quite strange, when checking if a number is a number FrancescoDiMuro and argumentum 2 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