?
Solved

Get text input in a perl script, if statements, looping

Posted on 2007-08-02
6
Medium Priority
?
950 Views
Last Modified: 2008-01-09
I have a perl script and I want to get text input from a user. I pseudo-coded what I want to do because I don't know any perl:

#!/usr/bin/perl
print "Hello World\n";

do
  (get text input here)
  if text = a then print "Monkey\n";
  if text = b then print "Donkey\n";
  if text = c then exit loop
loop
0
Comment
Question by:silentreproach
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
derelict_debris earned 1000 total points
ID: 19620540
#!/usr/bin/perl
print "Hello World\n";

my $input;
my $quit = 0;
while(!$quit) {
  $input = <STDIN>;
  print "Monkey\n" if $input eq 'a';
  print "Donkey\n" if $input eq 'b';
  $quit++ if $input eq 'c';
}

exit;

that should do it for you -- if you've got questions, just ask

HTH - derelict
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 1000 total points
ID: 19620555
print "Hello World\n";
while(1) {
      $text=<STDIN>;
      chomp($text);
      print "Monkey\n" if $text eq 'a';
      print "Donkey\n" if $text eq 'b';
      last if $text eq 'c';
}
0
 
LVL 1

Expert Comment

by:derelict_debris
ID: 19620556
oops!

add this line:
chomp($input);

after $input = <STDIN>;

otherwise your $input will have a '\n' at the end.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:silentreproach
ID: 19625387
I hope the points split is okay with you guys; I'm not fully sure of the protocol for awarding points.

Derelict was first, but Adam's solution seemed simpler and is the one I'm using.  Both worked for me, thanks!
0
 

Author Comment

by:silentreproach
ID: 19625631
I am having one small issue, I'd like to print the value of $text after the chomp statement, but this syntax doesn't work:

print "Hello World\n";
while(1) {
      $text=<STDIN>;
      chomp($text);
>>>print $text;
      print "Monkey\n" if $text eq 'a';
      print "Donkey\n" if $text eq 'b';
      last if $text eq 'c';
}
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19625707
print $text will print the text, but there will be no newline after it is printed.
You might want this:
    print "$text\n";
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

831 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