avidovi Posted January 30, 2016 Posted January 30, 2016 (edited) Hello friends, i need to extract valur from a text file, the text file containes the following data: True X=1405 Y=627 i need to get the X and y valuse. i used Local $imageSearchRes = FileRead("D:\SearchImageStatus.txt") $x = StringMid($imageSearchRes,8,4) $y = StringMid($imageSearchRes,14,4) Is there any clever way to do it? thanks Edited January 30, 2016 by avidovi
Bowmore Posted January 30, 2016 Posted January 30, 2016 Is there only one instance in the file or are there multiple values you want to extract. It would help if you could post an example of the text file. What you currently have may fail if there are different number of digits for to x and y values. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook
InunoTaishou Posted January 30, 2016 Posted January 30, 2016 (edited) If it's always formatted like that exactly then your stringmid would work fine, just as long as you use StringInStr to get the start and stop indexes of the number. If you don't wanna do that you could try something like this. #include <Array.au3> Global $val[] = ["True X=1405 Y=627", "False X=0 Y=0", "True X=24 Y=13", "True X=1920 Y=1080"] For $i = 0 to UBound($val) - 1 Local $split_val = StringSplit($val[$i], " ", $STR_NOCOUNT) Local $x = 0 Local $y = 0 Local $bool = False ExtractXY($split_val, $x, $y, $bool) MsgBox("", "Values", "Status = " & $bool & @CRLF & "X = " & $x & @CRLF & "Y = " & $y) Next Func ExtractXY(Const ByRef $aArray, ByRef $iX, ByRef $iY, ByRef $bBool) For $i = 0 to UBound($aArray) - 1 If (StringInStr($aArray[$i], "X")) Then $iX = StringSplit($aArray[$i], "=", $STR_NOCOUNT)[1] ElseIf (StringInStr($aArray[$i], "Y")) Then $iY = StringSplit($aArray[$i], "=", $STR_NOCOUNT)[1] ElseIf ($aArray[$i] = "True" or $aArray[$i] = "False") Then $bBool = $aArray[$i] EndIf Next EndFunc Using StringSplit. So it doesn't matter what order the string is, just as long as there's not spaces in the X=n or Y=n. You could also do something a little trickier in case the X/Y/Bool is mixed up and there's spaces between the X=n or Y=n #include <Array.au3> Global $val[] = ["True X=1405 Y=627", "False X = 0 Y=0", "True X = 24 Y = 13", "True X = 1920 Y=1080"] For $i = 0 to UBound($val) - 1 Local $x = 0 Local $y = 0 Local $bool = False ExtractXY($val[$i], $x, $y, $bool) MsgBox("", "Values", "Status = " & $bool & @CRLF & "X = " & $x & @CRLF & "Y = " & $y) Next Func ExtractXY($sString, ByRef $iX, ByRef $iY, ByRef $bBool) Local $split_string Local $index $sString = StringStripWS($sString, $STR_STRIPALL) If (StringInStr($sString, "X=")) Then $index = StringInStr($sString, "X=") + 1 $split_string = StringSplit($sString, "", $STR_CHRSPLIT + $STR_NOCOUNT) While ($index < UBound($split_string) and StringIsDigit($split_string[$index])) $iX &= $split_string[$index] $index += 1 WEnd EndIf EndFunc Edited January 30, 2016 by InunoTaishou
kylomas Posted January 30, 2016 Posted January 30, 2016 avidovi, Try this... #include <array.au3> local $str = "True X=1405 Y=627 False X=0 Y=0 True X=24 Y=13 True X=1920 Y=1080" $aRSLT = stringregexp($str,'(?i)(?:x|y)=(\d+)',3) _arraydisplay($aRSLT) kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
avidovi Posted January 31, 2016 Author Posted January 31, 2016 Kylomas, InunoTaishou thanks a lot i will try it and will keep u posted, thanks very much!
avidovi Posted January 31, 2016 Author Posted January 31, 2016 kylomas, Thank you very much, work perfect. i have defined x + y as in the array thanks again!!
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