Trina Espinoza wrote: > I have a file that I want to run multiple actions on. First, the file is > read in as one long string. I want to > substitute some words with others so I run a substitution. That seems to > change the data in the first > while, HOWEVER, when I run the second while loop on the same data to parse > my newly reformated file, I get the original file, instead of the newly > reformated file. . . > > Something like the code I'm using: > ----------------------------- > open(SOURCE, $file);
Make sure to check open's return value > > ###Run a substitution that breaks up my string into lines > while (<SOURCE>) { > s/something/\nsomething/g; You are modifying $_, but not writing the contents back to the file. You can open a temporary file, write the modified contents to it and at the end rename the temporary file as your original file. The other approach would be to use $^I (perldoc perlvar, perldoc perlrun, look for the -i flag, $^I is a mnemonic for the -i command line flag) and the 'null filehandle' (perldoc perlop) For e.g. local $^I = '~'; local @ARGV = ($file); while (<>) { s/something/\nsomething/g; print; } The module Tie::File will also interest you, I guess it comes with the standard distribution of perl 5.8.0 and onwards. You can install it from http://search.cpan.org if you don't have it. > > > } > #What I want it to parse a file, which at this point, shoud consist of > multiple lines. > #HOWEVER problem is I amI running this command on the same file > > while (<SOURCE>) { > parse SOURCE; > } > > ---------------------------------------------- > Is the best thing to save the outcome of the substitution into a variable > and read that variable in on the > second while or is there an easier way? Thanks in advance for the help :) > > -T > > e.g. > > while (<SOURCE>) { > $newFile = s/something/\nsomething/g; > } > #does this even work? > > while ($newfile) { This won't work as $newfile will contain the last modified line of the file when the while loop on the SOURCE filehandle finishes. > > parse stuff ; > } > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]