Hi KevinADC,
I want to understand what is missing or wrong with my script, not be handed a different script. Your script is great but it uses a for loop and I am using a while loop. Plus based on what I learned in class my code should be working so I know there has got to be one thing I am missing.
Your code causes me to have more questions - for example, why do you have to open the file two times once to read and once to write? Can you open it to write from the beginning and then stuff that into the array, modify the data, and print back to the file directly??? Here is an example of what I am saying and wondering why THIS does not work either:
<em># Rewrite of kevin code, why don't this work?</em>
open (INFILE, '<', $ARGV[0]) or die "Error writing to $ARGV[0]: $!\n";
@slurp = <INFILE>;
for ( @slurp )
{
s#<(/?)H1>#<$1H2>#g;
print INFILE $_;
}
close INFILE;
Mike,
You have several very good replies on perlguru but here is your code rewritten to work:
<em># first open file for reading only</em>
open (INFILE, $ARGV[0]) or die "Error $ARGV[0]: $!\n";
@slurp = <INFILE>;
close(INFILE);
<em>#now open file for overwriting</em>
open (OUTFILE, ">", $ARGV[0]) or die "Error $ARGV[0]: $!\n";
for (@slurp){
s#<(/?)H1>#<$1H2>#g;
print OUTFILE;
}
close OUTFILE;