bob wrote: > Hi > I wrote a program to append a number to an existing file. > but found a problem that i can't understand > > ******************************************************* > code1: > my $cnt= 0 > open FILE, ">>diff.txt"; > > while (<FILE>) { > if (match some condition) { > $cnt= $cnt+ 1; > } > } > > print FILE "cnt is: $cnt\n"; > close FILE; > ******************************************************* > the printed $cnt is 0. > > > but when write like this: > ***************************************************** > code2: > my $cnt= 0 > open FILE, "diff.txt"; > > while (<FILE>) { > if (match some condition) { > $cnt= $cnt+ 1; > } > } > close FILE; > > open FILE, ">>diff.txt" > print FILE "cnt is: $cnt\n"; > close FILE; > ***************************************************** > > the printed $cnt is the right num. > > why the 1st code cannot work properly? > thanks.
In the first case you have opened the file for append only so you cannot read from it. Do you really need to read what's already there? Anything you print to that filehandle will be appended to the existing contents as you describe. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/