How can I print some numbers during a loop

for example I have a while loop, and while the loop is running i want to print the numbers of times the loop has been repeated.

RTMF...

use strict;
use warnings;
my $x=0;
while($x<100){
	$x++;
	print "$x\n";
}
print "the loop ran $x times\n";

RTMF...

use strict;
use warnings;
my $x=0;
while($x<100){
	$x++;
	print "$x\n";
}
print "the loop ran $x times\n";

I wonder if terabyte would mind telling us how to code a few tools, including calculators without printing during a loop.:-/

If I were the OP, I'd mark this one solved.

I wonder if terabyte would mind telling us how to code a few tools, including calculators without printing during a loop.:-/

If I were the OP, I'd mark this one solved.

LOL?
are you trying to unmask my lack of Perl knowledge?
because I already stated I started learning perl few days ago.

BTW thanks for the answer but what I was wondering was if it is possible to print messages while the loop is running not when it finishes.

Because I coded a small script that runs for about 5 mins and It would be a nice detail to print the number of seconds until the process finishes

I think that is not possible thanks anyways.

LOL?
are you trying to unmask my lack of Perl knowledge?
because I already stated I started learning perl few days ago.

BTW thanks for the answer but what I was wondering was if it is possible to print messages while the loop is running not when it finishes.

Because I coded a small script that runs for about 5 mins and It would be a nice detail to print the number of seconds until the process finishes

I think that is not possible thanks anyways.

I didn't mean to disparage your knowledge of perl and maybe I misunderstood your original question. What I thought you asked originally was "while the loop is running i want to print the numbers of times the loop has been repeated" and IMO mitchems answered that question. Now you ask how to print the number of seconds left before the process finishes. In my opinion that is a different question. Of course if you don't know in advance how much time a process is going to take then you probably cannot say how much longer the process will take while the process is running. But you would need to provide us with more information about the process before we can say whether what you want to do is possible. Why not start a new thread to ask it and mark this one solved?

If you use Time SoFar you can calculate this very easily:

use strict;
use warnings;
$ENV{TZ}="EDT";
use Time::SoFar qw( runtime runinterval figuretimes );
my $elapsed = runtime();
print "Start time $elapsed\n";
my $x=0; 
while($x<1000000000){
	#waiting
	$x++;
}
my $sincethen = runinterval(1);
print "Time since then $sincethen\n";

Output:

Start time 0:00
Time since then 0:00:01:25

That's days, hours, minutes, seconds.

I was looking for something like autoflush

adding this line of code to my script solved my problem

local $| = 1;

Thanks for the answers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.