Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange print() problem. anyone seen this before?

Status
Not open for further replies.

m4trix

Vendor
Jul 31, 2002
84
CA
I've got a piece of code that runs through each element in an array doing stuff. It can take a while depending on the size of the array, so I figured I'd be nice and output some percentages to make sure the user knows that something is infact happening... so I stuck this in the loop. ($percent is initiated to 1)

[tt]
if($percent <= 10 && $cur == int($total/10*$percent)){
print &quot; &quot;.$percent.&quot;0\%&quot;;
$percent++;
}
$cur++;
[/tt]

Now this SHOULD output:

[tt] 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%[/tt]

and it works fine in any C program I've ever written,
but instead it doesn't output anything. HOWEVER, if I change the code to:

[tt]
if($percent <= 10 && $cur == int($total/10*$percent)){
print &quot; &quot;.$percent.&quot;0\%\n&quot;;
$percent++;
}
$cur++;
[/tt]

(added /n)
it works fine, outputting
[tt]
10%
20%
30%
...
100%
[/tt]

So it only works if I stick a \n in there... why would this be? any way around this? thanks
 
The output is being buffered. When you print a newline, &quot;\n&quot;, the buffer is flushed and the line is printed. Without the newline the output is buffered until the buffer is full or an EOL character is printed.

To turn on autoflushing for STDOUT, add the following before the loop is entered:
Code:
$| = 1;
jaa
 
Or append &quot;\n&quot; when you've finished your printline?
 
Note that if using a win32 machine, that autoflushing doesn't work. You have to append &quot;\n&quot; to the line for it to print while your script is running.

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top