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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flushing STDIN

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Quick question:<br><FONT FACE=monospace><br>while (&lt;STDIN&gt; ne &quot;quit&quot;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chop($data = &lt;STDIN&gt;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print STDOUT &quot;standard in was: $data\n&quot;;<br>}<br></font><br><br>The above reads whatever the user types in and echo's it to the screen. However, there is a problem where only every other data input will get captured i.e.<br><br><FONT FACE=monospace><br>&gt; ./stdin.pl<br>1<br>2<br>standard in was: 2<br>3<br>4<br>standard in was: 4<br>5<br>6<br>standard in was: 6<br>7<br>8<br>standard in was: 8<br>9<br>10<br>standard in was: 10<br></font><br><br>How can I ensure that everything written to STDIN will get placed in $data?<br><br>Cheers<br>Loon<br><br>
 
You are listening to the STDIN pipe twice.&nbsp;&nbsp;Each time you use <b>&lt;STDIN&gt;</b>, you are listening for keyboard input.&nbsp;&nbsp;That is happening 2 times in your snippet.<br><br>a minor tweak will fix the problem....make your condition check the value, ($data) recieved from STDIN on the previous loop.<br><br><b>#!perl<br>while ($data ne &quot;quit&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;chop($data = &lt;STDIN&gt;);<br>&nbsp;&nbsp;&nbsp;&nbsp;print STDOUT &quot;standard in was: $data\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></b> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top