Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

how to print only 47 and another number will rejected and when user give 47 that number only print ?

by virudinesh (Acolyte)
on Dec 10, 2013 at 14:55 UTC ( #1066441=perlquestion: print w/ replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

how to print only 47 and another number will rejected and when user give 47 that number only print ?

Comment on how to print only 47 and another number will rejected and when user give 47 that number only print ?
Re: how to print only 47 and another number will rejected and when user give 47 that number only print ?
by marto (Chancellor) on Dec 10, 2013 at 15:02 UTC
    #!/usr/bin/perl use strict; use warnings; print "Enter a number:"; my $input = <STDIN>; chomp $input; print "47\n" if ( $input == 47 );

    This probably isn't what you want, I can't tell exactly what you're looking for based on what you've asked here and in the CB. This sounds like a homework assignment, I suspect the best course of action would be to study your notes, speak to your tutor or other students. See also How do I post a question effectively?.

      marto:

      OVERLY COMPLEX! I think you could have simplified it to[1]:

      #!/usr/bin/perl use strict; use warnings; print "Enter a number:"; my $input = <STDIN>; print "47\n";

      In fact, when I read your response, I was transported back in time to high school. My friends and I formed a small informal computer club where we each created programs for the amusement of others. My friend Tony created "Super Star Trek 4.71" which amused the hell out of me. I no longer have the source code to it, as little output from the TTY[2] still lingers at the house. But a close approximation in perl[3] would be like:

      #!/usr/bin/perl use strict; use warnings; use Time::HiRes 'usleep'; $|=1; my $A; sub PR1NT { my $msg = shift; my @chars = split //,$msg; while (defined(my $t = shift @chars)) { usleep 100000; print $t; } } sub INPUT { print "? "; my $a = <>; return $a; } L0010: PR1NT "WELCOME TO SUPER STAR TREK VERSION 4.71\n"; L0015: PR1NT "\n"; L0020: PR1NT "A SPACE ADVENTURE SURE TO THRILL THE SOUL!\n"; L0030: PR1NT "COPYRIGHT 1979 BY ANTHONY D. ENNIS\n"; L0040: PR1NT "\n"; L0050: PR1NT "WOULD YOU LIKE INSTRUCTIONS"; L0060: $A = INPUT(); L0070: PR1NT "\n"; L0080: PR1NT "WHAT IS YOUR FAVORITE NUMBER"; L0090: $A = INPUT(); L0100: PR1NT "YOU WIN!!!!\n"; L0110: PR1NT "\n"; L0120: PR1NT "WOULD YOU LIKE TO PLAY AGAIN"; L0130: $A = INPUT(); L0140: PR1NT "\n"; L0150: goto L0010;

      Notes:

      [1] I could've left out the use warnings/errors boilerplate, but we wouldn't want to give people bad habits, would we?

      [2] Last year at about this time, I dug up a printout of his D&D program from the same era and gave it to him. Now he has an Android version on his phone. He needs to give me a copy of it! I hate to think of how many rolls of paper our computer club wasted on that game. ;^)

      [3] Yep, I actually wasted 15 minutes of my life recreating Tony's joke game.

      The rendition of his game here is from memory, so it's obviously not exactly the same. But I'm sure it's not far from the mark!

      Update: I forgot the "just kidding" note. This was just a joke response because marto triggered an ancient memory.

      Update: Had some "junk" in the star trek program, now removed. (Looks like I pasted a copy of the first code snippet into the second one.) Thanks to N-Wing for catching it! Also made a couple tweaks to make it a little closer to the original.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re: how to print only 47 and another number will rejected and when user give 47 that number only print ?
by davido (Archbishop) on Dec 10, 2013 at 16:28 UTC

    use IO::Prompt::Hooked qw(prompt); my $input = prompt( message => 'Please enter 47:', validate => qr/^47$/, ); print "You entered: $input\n";

    Sample run:

    Please enter 47: a Please enter 47: b Please enter 47: 42 Please enter 47: 47 You entered: 47

    I feel it coming.... "But I can't use modules.":

    • Why not?
    • Then why are you using Perl?
    • Then you should look at the fairly trivial source code for IO::Prompt::Hooked and see how it works. Or at minimum, read and comprehend perlintro.

    Dave

Re: how to print only 47 and another number will rejected and when user give 47 that number only print ?
by Kenosis (Priest) on Dec 10, 2013 at 16:45 UTC

    Here's another option:

    use strict; use warnings; my $num; do { chomp( $num = <> ); } until $num eq '47'; print $num;

    This keep's doing the code block (getting input from STDIN) until $num is 47. $num's printed only if it's 47. Why a eq instead of a ==? If 'asdf' is entered when using a ==, you'll get an Argument "asdf\n" isn't numeric in numeric eq (==) at ... messsage, but not if eq is used.

    Did you want stealth? The following will not echo anything to the screen until 47 is entered, and then 47 will be printed:

    use strict; use warnings; use Term::ReadKey; my $num; ReadMode 'noecho'; do { chomp( $num = ReadLine(0) ); } until $num eq '47'; ReadMode 'restore'; print $num;

    Hope this helps!

Re: how to print only 47 and another number will rejected and when user give 47 that number only print ?
by Lennotoecom (Pilgrim) on Dec 10, 2013 at 18:50 UTC
    sub{print "entered $_\n" if /^47$/}->() while <STDIN>;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1066441]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-10-24 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls