Cathy wrote:
my $lines = 0; my $current_line = 0; my $percentage; my $percentage_new; open(my $FILE, "<", @ARGV[0]) or die "Can't open log file: $!"; while (sysread $FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } print "$lines lines\n"; close $FILE or die "$in: $!"; open(my $FILE, "<", @ARGV[0]) or die "Can't open log file: $!"; while(<$FILE>) { $current_line += 1; $percentage_new = sprintf("%d",$current_line / $lines * 100); #$delta = $percentage_new - $percentage; #print "$delta\n"; if($percentage_new - $percentage > 0) { print "$current_line\n"; $percentage = $percentage_new; } } Hi all, This is my first post and this could be easy question. In a source code above, it prints out total line number first, and in a loop , it prints out the line number whenever the percentage increases. The problem is, when I put \r instead of \n, nothing is printed. But if I remove 'if' statement, it prints out normally with \r. Does anyone know what is wrong?
Try it like this instead: #!/usr/bin/perl use warnings; use strict; use Fcntl ':seek'; $|++; # autoflush ON open my $FILE, '<', $ARGV[ 0 ] or die "Can't open $ARGV[0]: $!"; my $lines = 0; while ( read $FILE, my $buffer, 4096 ) { $lines += $buffer =~ tr/\n//; } print "$lines lines\n"; seek $FILE, 0, SEEK_SET; my $percentage = 0; while ( <$FILE> ) { my $percentage_new = int( $. / $lines * 100 ); #my $delta = $percentage_new - $percentage; #print "$delta\n"; if ( $percentage_new - $percentage > 0 ) { print "$percentage_new\r"; $percentage = $percentage_new; } } print "\n"; __END__ John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/