problem with print when \r is used.

C

Cathy

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 I'm sorry if this is too easy question.
I wanted to indicate the progress of line number while reading a file.

In a source code above, it prints out total line number first, and
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?
 
R

RedGrittyBrick

Cathy said:
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 I'm sorry if this is too easy question.
I wanted to indicate the progress of line number while reading a file.

In a source code above, it prints out total line number first, and
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?

The standard output is line-buffered bu default - nothing is actually
output until a newline character. The way I turn off line-buffering is
by setting the special "autoflush" variable $| to a non-zero value. It
should be described somewhere in `perldoc perlvar` but see `perldoc -q
buffer`.
 
J

John W. Krahn

Cathy said:
This is my first post and I'm sorry if this is too easy question.
I wanted to indicate the progress of line number while reading a file.

In a source code above, it prints out total line number first, and
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?

Didn't you like the answer I gave you on the Perl beginners mailing list?


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top