williamk Posted June 12, 2007 Posted June 12, 2007 Have a piece of code that is supposed to check a text file for every line that ends in "001". It should take those lines and write them to a new file. So I have a text file that reads: 07566276.001 07566276.002 07566276.003 07566276.004 07566276.005 07566276.006 07566307.001 07566307.002 07566307.003 07566307.004 07566307.005 07566311.001 07566311.002 07566311.003 07566311.004 07566311.005 The new file ($file2) should read: 7566276 7566307 7566311 If I take out the StringRegExp the code works. It just writes and trims the whole list, so I know It is something with that statement. What am I doing wrong. $file = FileOpen("C:\Corrected_Scans\Before\cslist.txt", 0) $file2 = FileOpen("C:\Corrected_Scans\cslist2.txt", 1) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $chars = FileReadLine($file) If @error = -1 Then ExitLoop $chars = StringRegExp($chars, ".........001") If @error = 1 then $chars = StringTrimLeft($Chars, 1) $chars = StringTrimRight($Chars, 4) $chars = StringStripCR($Chars) $chars = stringstripws($chars, 2) filewriteline ($file2, $chars) endif Wend FileClose($file) FileClose ($file2)
Moderators SmOke_N Posted June 12, 2007 Moderators Posted June 12, 2007 (edited) I have no idea what you are really asking... but if you know that StringRegExp() returns a 0 base array.... this should work from how I am interpreting it. #include <array.au3> $sRead = FileRead(@HomeDrive & '\Corrected Scans\cslist2.txt') $a001 = StringRegExp($sRead, '(?s)(?i)0(\d+)\.001', 3) _ArrayDisplay($a001) Edited June 12, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
herewasplato Posted June 12, 2007 Posted June 12, 2007 (edited) SmOke_N, Sure... give the OP what he asked for.... :-) ;opens file and splits it into an array $FileArray = StringSplit(FileRead("cslist.txt"), @CRLF) ;opens output file in the append mode $file2 = FileOpen("cslist2.txt", 1) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;loops thru each array element looking for .001 For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then FileWriteLine($file2, StringLeft($FileArray[$i], 8)) Next FileClose($file2)Edit: It worked for me when I tested it. Watch the single line IF statement as it may wrap in your forum display. Edit2: changed/tested code to look for ".001" Edited June 12, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size]
Rick Posted June 12, 2007 Posted June 12, 2007 (edited) dont know if this helps, but... $Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF) For $x = 1 to $Lines[0]-1 $Split=StringSplit($Lines[$x],".") if IsArray($Split) And $Split[2] = "001" then FileWrite("File2.txt",$Split[1] & @CRLF) Next Edited June 12, 2007 by Rick Who needs puzzles when we have AutoIt!!
Moderators SmOke_N Posted June 12, 2007 Moderators Posted June 12, 2007 Think you two forgot about the leading zero Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Rick Posted June 12, 2007 Posted June 12, 2007 (edited) oops $Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF) For $x = 1 to $Lines[0]-1 $Split=StringSplit($Lines[$x],".") if IsArray($Split) AND $Split[2] = "001" then FileWrite("File2.txt",StringTrimLeft($Split[1],1) & @CRLF) Next Edited June 12, 2007 by Rick Who needs puzzles when we have AutoIt!!
herewasplato Posted June 12, 2007 Posted June 12, 2007 Think you two forgot about the leading zero Like I said - you keep given the OP what they ask for. ;opens file and splits it into an array $FileArray = StringSplit(FileRead("cslist.txt"), @CRLF) ;opens output file in the append mode $file2 = FileOpen("cslist2.txt", 1) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;loops thru each array element looking for .001 For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then If StringLeft($FileArray[$i], 1) = "0" Then FileWriteLine($file2, StringMid($FileArray[$i], 2, 7)) Else FileWriteLine($file2, StringLeft($FileArray[$i], 8)) EndIf EndIf Next FileClose($file2) [size="1"][font="Arial"].[u].[/u][/font][/size]
williamk Posted June 12, 2007 Author Posted June 12, 2007 oops $Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF) For $x = 1 to $Lines[0]-1 $Split=StringSplit($Lines[$x],".") if IsArray($Split) AND $Split[2] = "001" then FileWrite("File2.txt",StringTrimLeft($Split[1],1) & @CRLF) Next Hey Rick, This code returns an error that says "Array Variable has incorrect number of subscripts or subscript dimension range exceeded." I have no idea what that means. Here is what I tried to run: $Lines=StringSplit(StringReplace(FileRead("C:\Corrected_Scans\Before\cslist.txt"),@CR,""),@LF) For $x = 1 to $Lines[0]-1 $Split=StringSplit($Lines[$x],".") if IsArray($Split) AND $Split[2] = "001" then FileWrite("C:\Corrected_Scans\cslist2.txt",StringTrimLeft($Split[1],1) & @CRLF) Next
/dev/null Posted June 12, 2007 Posted June 12, 2007 Hey Rick,This code returns an error that says "Array Variable has incorrect number of subscripts or subscript dimension range exceeded."erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??! __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
williamk Posted June 12, 2007 Author Posted June 12, 2007 Like I said - you keep given the OP what they ask for. ;opens file and splits it into an array $FileArray = StringSplit(FileRead("cslist.txt"), @CRLF) ;opens output file in the append mode $file2 = FileOpen("cslist2.txt", 1) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;loops thru each array element looking for .001 For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then If StringLeft($FileArray[$i], 1) = "0" Then FileWriteLine($file2, StringMid($FileArray[$i], 2, 7)) Else FileWriteLine($file2, StringLeft($FileArray[$i], 8)) EndIf EndIf Next FileClose($file2) Right on. This works perfecty. The only thing I really don't understand is the lines: For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then Any chance you can explain them to me a little or point me to a reference. I have read the docs section on arrays, but am still struggling to uderstand that concept. Variables I get, arrays I don't. By the way, what is an OP? Thanks.
Moderators SmOke_N Posted June 12, 2007 Moderators Posted June 12, 2007 (edited) Like I said - you keep given the OP what they ask for. ;opens file and splits it into an array $FileArray = StringSplit(FileRead("cslist.txt"), @CRLF) ;opens output file in the append mode $file2 = FileOpen("cslist2.txt", 1) If $file2 = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;loops thru each array element looking for .001 For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then If StringLeft($FileArray[$i], 1) = "0" Then FileWriteLine($file2, StringMid($FileArray[$i], 2, 7)) Else FileWriteLine($file2, StringLeft($FileArray[$i], 8)) EndIf EndIf Next FileClose($file2) Edited June 12, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
williamk Posted June 12, 2007 Author Posted June 12, 2007 erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??!Yes it is. Just thought you would like to know. Besides, 2 solutions are better than one. I learn more too
Moderators SmOke_N Posted June 13, 2007 Moderators Posted June 13, 2007 erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??!Actually, reading through the rest of the posts got me thinking that mine wasn't the "best" solution.... I don't like the "0" leading digits thing with no minimum on the characters other than "1". I think this is smarter if you know that there will always be 7 digits before the decimal and after the leading zero....#include <array.au3> $sRead = FileRead(@HomeDrive & '\Corrected Scans\cslist2.txt') $a001 = StringRegExp($sRead, '(?s)(?i)0(\d{7})\.001', 3) _ArrayDisplay($a001) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
herewasplato Posted June 13, 2007 Posted June 13, 2007 (edited) Right on. This works perfecty. The only thing I really don't understand is the lines: For $i = 1 To $FileArray[0] If StringMid($FileArray[$i], 9, 4) = ".001" Then Any chance you can explain them to me a little or point me to a reference. I have read the docs section on arrays, but am still struggling to uderstand that concept. Variables I get, arrays I don't. By the way, what is an OP? Thanks.You are the OP. :-) OP = the person who made the Original Post or the Original Post itself - depending on how it is used. When you use StingSplit, it puts the info into an Array like this: $FileArray[0] = 16 <--- this will be how many numbers are in your input file $FileArray[1] = 07566276.001 $FileArray[2] = 07566276.002 ---------- EDIT: So For $i = 1 To $FileArray[0] becomes For $i = 1 To 16 Each time thru the loop, $i increments by 1 (by default) This allows you to work thru an array with ease. ---------- The first time thru the loop, this line: StringMid($FileArray[$i], 9, 4) will be: StringMid($FileArray[1], 9, 4) It [StringMid] is reading this number... 07566276.001 123456789 starting at the 9th position [which is the decimal] and returning 4 characters [.001] so, StringMid($FileArray[1], 9, 4) = ".001" becomes ".001" = ".001" and If ".001" = ".001" then write that line to the file* *or you might want to trim the leading 0 hope this helps Edit2: [in bold] Edit3: hope that was not too insulting... I was unsure about what you were unsure about. Edited June 13, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size]
/dev/null Posted June 13, 2007 Posted June 13, 2007 By the way, what is an OP? try this: #include <file.au3> $file_in = @HomeDrive & '\Corrected Scans\cslist2.txt' $file_out = @HomeDrive & '\Corrected Scans\cslist2_out.txt' $sRead = FileRead($file_in) $a001 = StringRegExp($sRead, '(?s)(?i)0(\d+)\.001', 3) _FileWriteFromArray($file_out,$a001) __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
/dev/null Posted June 13, 2007 Posted June 13, 2007 Actually, reading through the rest of the posts got me thinking that mine wasn't the "best" solution.... I don't like the "0" leading digits thing with no minimum on the characters other than "1".with the information given, it was the best assumption ;-) __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
amel27 Posted June 13, 2007 Posted June 13, 2007 $a001 = StringRegExp($sRead, '(?s)(?i)0(\d+)\.001', 3)oÝ÷ Ûú®¢×+)jÛ^®z''è®È§W¥y§bޮɷ+)jÛ^®y¨æ©¦º)®&y«¢+ØÀÌØíÀÀÄôMÑÉ¥¹IáÀ ÀÌØíÍI°Ìäì ý´¥xÀ¨ ÀäÈí¬¤ÀäÈì¸ÀÀÄÌäì°Ì
/dev/null Posted June 13, 2007 Posted June 13, 2007 (edited) actually this pattern is more appropriate: http://www.autoitscript.com/forum/index.ph...st&p=356431 EDIT: I don't know where the strange characters in my reply come from. Seems to be a forum bug... Edited June 13, 2007 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
Gif Posted June 13, 2007 Posted June 13, 2007 (edited) actually this pattern is more appropriate: http://www.autoitscript.com/forum/index.ph...st&p=356431 EDIT: I don't know where the strange characters in my reply come from. Seems to be a forum bug... testing reply and some code: #NotrayIcon msgbox(0, "123", "12345", 3) sleep(100) exit Edited June 13, 2007 by c4nm7
/dev/null Posted June 13, 2007 Posted June 13, 2007 testing replyplease reply to this post: http://www.autoitscript.com/forum/index.ph...st&p=356501 __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
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