987learner Posted November 24, 2008 Posted November 24, 2008 I have a txt file that I'm using to conduct a search for a match string "datasource.0.ch_id". Presently, this string is in line 91 of the txt file. The actual string in line 91 is datasource.0.ch_id=168486 I wish extract 168486 out, base on matching "datasource.0.ch_id" string. The problem is this string can be in line 90 to 110. How do I get autoit to detect datasource.0.ch_id from line 90 to 110. There is only 1 instance of this string. Have only started with basic code. I only how to perform 2 possible checks (line 91 or line 93) using if... then..else. As there are 20 possibility in 90 to 110 range, I think a more efficient code should be use. But I have no clue how to do that. Could anyone kindly help me with it? All these check will only return 1 value (as in the msgbox value). I have to figure out how to get other values in this similar sense (another 11 of them). Thanks. $file =FileOpen ( "C:\customprogram\client\utility_context.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $chartid=FileReadLine ($file,91); read chart id on line 91 $chartchk=StringRegExp ( $chartid, "datasource.0.ch_id" , 0 ) ;MsgBox(0, "Chartchk1", $chartchk); 0 imply string not match , 1 imply string match if $chartchk=0 then $chartid=FileReadLine ($file,93) $chartchk=StringRegExp ( $chartid, "datasource.0.ch_id" , 0 ) ;ElseIf $chartchk=0 then ; $chartid=FileReadLine ($file,109) $Fchartid=StringMid ( $chartid, 20, 6 ) else $Fchartid=StringMid ( $chartid, 20, 6 ); base on line 93 input endif FileClose($file) MsgBox(64,"Details",$Fchartid)
dbzfanatic Posted November 24, 2008 Posted November 24, 2008 Try using _FileReadToArray(), StringInStr(), StringSplit() and a For ... Next loop to go through the array. Go to my website. | My Zazzle Page (custom products)Al Bhed Translator | Direct linkScreenRec ProSimple Text Editor (STE) [TUTORIAL]Task Scheduler UDF <--- First ever UDF!_ControlPaste() UDF[quote name='renanzin' post='584064' date='Sep 26 2008, 07:00 AM']whats help ?[/quote]
987learner Posted November 24, 2008 Author Posted November 24, 2008 Thanks, but I'm unsure how to code it using your suggested method. I'm pretty new to these. Never use _FileReadToArray() coding before.
Malkey Posted November 24, 2008 Posted November 24, 2008 (edited) I would be interested to see if this works for you.$file = FileOpen("C:\customprogram\client\utility_context.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf For $line = 90 To 110 If StringInStr(FileReadLine($file, $line), "datasource.0.ch_id=") Then $Fchartid = StringRegExpReplace(FileReadLine($file, $line), "(\D.*|\d.*|)(datasource.0.ch_id)=(\d*)(\D.*|\d.*|)", "$3") $Online = $line EndIf Next FileClose($file) MsgBox(64, "Details", " On line " & $Online & " value = " & $Fchartid)Edit Replaced RegExp "(\S?)datasource.0.ch_id=(\d+?)" with"(\D*)(datasource.0.ch_id)=(\d*)(\D*)"Allows for Text before and after search string.Edit again: Replaced above with "(\D.*\d.*)(datasource.0.ch_id)=(\d*)(\D.*\d.*)"If the search line is "line 19 asd saddatasource.0.ch_id=168486dfv tyu 66w"$Fchartid = 168486This is the last edit: Replaced above with "(\D.*|\d.*|)(datasource.0.ch_id)=(\d*)(\D.*|\d.*|)"Now if line is as above or line is "datasource.0.ch_id=168486" when $Fchartid = 168486 Edited November 24, 2008 by Malkey
987learner Posted November 24, 2008 Author Posted November 24, 2008 Malkey, 1000 thanks! Works like a charm Output as follows:Now, I need to understand your code by reading the help file for the StringRegExpReplace. 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