club Posted April 4, 2012 Share Posted April 4, 2012 (edited) My goal is to parse a source text file and if any lines contain a hyphen then write that line to a new txt file. So the resulting product file will be a subset of the first file, but only lines that contain a hyphen. Here's what I got: $input = FileOpen("C:tempsource.txt", 0) $output = FileOpen("C:tempdone.txt", 2) Do $line = FileReadLine($input) If StringInStr($line, "-") Then FileWriteLine($output, $line) Until @error = -1 FileClose($input) FileClose($output) Problem is, my resulting file only contains the first instance of a line with a hypen, which is repeated over and over again. It's as if the script isnt moving to the next line the 2nd time it executes FileReadLine. The documentation says FileReadLine should automatically increment and read the next line in an opened file, so there shouldn't be any need to set up a counter to guide it. Any ideas why I'm getting this problem then? What simple thing am I missing? Much TiA. Edited April 4, 2012 by club Link to comment Share on other sites More sharing options...
club Posted April 4, 2012 Author Share Posted April 4, 2012 Ah nevermind. Im dumb. My actual variables are named different from the example I posted and I typo'd one of them. All works as expected now. Link to comment Share on other sites More sharing options...
club Posted April 4, 2012 Author Share Posted April 4, 2012 (edited) Ok, maybe not as expected. Seems the script freezes consistantly at some point. I thought it was hitting some character that was freaking it out but that doesn't seem to be the case. For example, if I open the output txt file it seems the script froze in mid-write. It partially wrote a line it found a hyphen in, but the line abruptly ends when there was more to that line in the input file. But then it gets even weirder. If I right click the script's icon and force it to exit, the output file instantly becomes complete. So it was working, but like stuck somehow. All those FileWriteLine's were like stuck in memory or something. And why would they suddenly work when I force the script to stop. Doesnt that just end the script immediately and dump whatever it was working on in memory? I mean, I guess it's working in a roundabout way... I wait till it seems to be frozen and then manually stop it and the output is as desired. But what is going on here? I've added a Sleep in my loop to see if that would help, but it seems to make no difference. Edited April 4, 2012 by club Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 4, 2012 Moderators Share Posted April 4, 2012 club,I see a major problem with that script - you are checking for @error for the wrong function. You want to trap it after the FileReadLine return - at the moment you are checking the FileWriteLine return. This works for me:$input = FileOpen("source.txt", 0) $output = FileOpen("done.txt", 2) While 1 $line = FileReadLine($input) If @error = -1 Then Exitloop If StringInStr($line, "-") Then FileWriteLine($output, $line) Wend FileClose($input) FileClose($output)That might also have something to do with your later problem - although I am not sure why. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
club Posted April 4, 2012 Author Share Posted April 4, 2012 (edited) I see now when I force the script the exit it doesnt just drop everything and die. It must do some housekeeping, such as close any opened files. This must be while my output file suddenly gets filled out as it should when I exit the script. It wasnt that the script was "freezing", just failing to exit the loop. Well definitely I appreciate your input. But I'm so OCD and my college instructors hammered into my brain that forcing a break out of a loop instead of using the loop's specified exit conditions being satisfied is a no-no. Or at least, I wouldve gotten dinged for such, gradewise. Edited April 4, 2012 by club Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 4, 2012 Moderators Share Posted April 4, 2012 club, forcing a break out of a loop instead of using the loop's specified exit conditions being satisfied is a no-no Then do it this way: Do $line = FileReadLine($input) ; Save the @error returned from this function $iError = @error If StringInStr($line, "-") Then FileWriteLine($output, $line) Until $iError = -1 It is functionally identical - but I would criticize this code for using an unnecessary variable. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
club Posted April 4, 2012 Author Share Posted April 4, 2012 I had just done something very similar right before I refreshed this thread. Does the trick. You're right also, I guess just a personal preference in the end. Thanks again! Link to comment Share on other sites More sharing options...
Bowmore Posted April 4, 2012 Share Posted April 4, 2012 A slight variation on the example Melba23 gave you. $input = FileOpen("source.txt", 0) $output = FileOpen("done.txt", 2) $line = FileReadLine($input) While Not @error If StringInStr($line, "-") Then FileWriteLine($output, $line) $line = FileReadLine($input) Wend FileClose($input) FileClose($output) "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 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