Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to flush the input buffer ?

Posted on 2002-04-25
11
Medium Priority
?
4,596 Views
Last Modified: 2012-05-04
This is the code to get a y/n but when I make a subsequent call it does not ask for the input !

#! /in/local/bin/perl
sub demande {
my $in=0;
system("echo \"Have u modified \"");
$in = lc(getc);
$in  =~ s/ //ge;
$in  =~ s/\n//ge;
if ($in ne "y")
{
system("echo \"U are out NO BOSS .... $in\"");
exit;
}
else
{
system("echo \"U are in YES BOSS .... $in\"");
}
}
#end od demande
########MAIN#########
demande();
demande();


thanks
0
Comment
Question by:anil27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6969077
anil27,

".. code to get a y/n but when I make a subsequent call it does not ask for the input !..."

What exactly are you trying to do? Can you explain in plain english what you are trying to acheive?

Let me know.

This will help you get a more accurate answer, faster.

Meanwhile, here is a sample script that will be run from the command prompt.
This script will ask the user for input.

If input is Y or y, then it will print a "YES" message.
If input is N or n, then it will print a "NO" message.
if input is anything else it will print a "INVALID" message.

#!/in/local/bin/perl

$|++; ## Disable output buffering

print "Enter your reply ";
$ans=<STDIN>; ##  Read the users input

##  Remove the newline character
chomp($ans);

if ($ans=~ /^y$/i){ ##  Y or y entered!!
  print "YES\n";
}elsif ($ans=~ /^n$/i){ ##  N or n entered!!
  print "NO\n";
}else{
  print "INVALID\n";
}
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 6970060
> $|++; ## Disable output buffering

hmm, and if someone decided to set perl's default of $|=(-1); ??
I would recommend to use $|=1;

anil27, getc() gets the first typed character only, so your second call to demande() gets the newline (or carriage return) from the previous one, probably not what you want.
0
 

Author Comment

by:anil27
ID: 6970492
This is a duplicate question. It has already been answered by maneshr.
thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 6970549
$|++ is the same as $|=1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970571
ozo, see my comment. I know I'm pedantic, sometimes, but I disagree that  $|++  "is the same" as  $|=1

Also, with $|!=0 only STDOUT will be flushed (according to man-pages), I don't know of a method to flush STDIN (probably with select(), never tried such a hack).
0
 

Author Comment

by:anil27
ID: 6970581
I agree ahoffman with both of ur points. ! $| = 1 is reqd to be 100% sure, but by default it is 0 so $|++ will work in most cases.
I have put the comment for input buffer flush although I accepted the answer,as STDIN solved my problem.
input buffer is still NOT CLEARED.
regards
0
 
LVL 84

Expert Comment

by:ozo
ID: 6970634
$|++ is 100% sure
$| = -1 is the same as $| = 1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970661
it's a pedantic discussion, anyway:

> $|++ will work in most cases
agreed

> $| = -1 is the same as $| = 1
agreed

> $|++ is 100% sure
disagreed (if the goal is to get $|!=0 )

2 examples:
  1) the default is set to -1 somehow, somewhere, ..
  2) if someone uses $|++, I assume that there is also $|--, in this case you do not know the value of $|, usually, and it may be -1 when you call $|++, accidently

IMHO, $|=1 is 100% sure, 'cause it it defensive programming
0
 
LVL 84

Expert Comment

by:ozo
ID: 6970670
#ahoffman, try this:
$| = -1;
print "$|\n";
$|++;
print "$|\n";
$|--;
print "$|\n";
$|--;
print "$|\n";
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970682
opps, underestimated perl's sophisticated handling of booleans ;-)
But is the result of following obvious? IMO no:

$|++;$|--;$|--;$|--;print $|;
0
 

Author Comment

by:anil27
ID: 7553065
Duplicate answer but it is still lyng there so accepted.
thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question