eof in perl- going in infinite loop -
when checking eof
condition being false, it's going in infinite loop. here doing.
tie @lines, 'tie::file' , "abc.txt" or die; (my $i=0; ; $i++) { # if put if(!eof())condition instead of below one, # goes in infinite loop. may know why? if ($lines[$i] =~ /^[\s]*hello/) { splice @lines, $i, 1, "#$lines[$i]", "hi"; last; } #below eof condition working if(eof){ last; } }
an eof
without argument uses last file read. using eof()
empty parentheses different. refers pseudo file formed files listed on command line , accessed via <>
operator.
edit:
normally dont need use eof
, because input operators typically return undef
when run out of data, or if there error.
code:
maybe problem is, dont use 2nd condition of for
loop.
tie @lines, 'tie::file' , "abc.txt" or die; for(my $i=0;$i<=$#lines; $i++) { if ($lines[$i] =~ /^[\s]*hello/){ splice @lines, $i, 1, "#$lines[$i]", "hi"; last; } }
Comments
Post a Comment