Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Multiple Input help

by starface245 (Novice)
on Feb 04, 2011 at 20:10 UTC ( #886286=perlquestion: print w/replies, xml ) Need Help??
starface245 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/local/bin/perl use strict; use warnings; my ($userinput1, $userinputer2); $userinput1 = <STDIN>; #first name $userinput2 = <STDIN>; #last name print "Hello $userinput1_$userinput2";
Basically the code does not work right now because of the underscore. I need to have the underscore there. The display I want is Hello John_Stane

Replies are listed 'Best First'.
Re: Multiple Input help
by kennethk (Abbot) on Feb 04, 2011 at 20:15 UTC
    Your issue is arising because the first variable Perl is trying to interpolate is read as $userinput1_. You can avoid this issue by wrapping your variable name in curly brackets:

    #!/usr/local/bin/perl use strict; use warnings; my ($userinput1, $userinput2); $userinput1 = <STDIN>; #first name $userinput2 = <STDIN>; #last name print "Hello ${userinput1}_$userinput2";

    Note I fixed a typo in your variable names. As well, given your intended output, you likely want to chomp your inputs:

    #!/usr/local/bin/perl use strict; use warnings; my ($userinput1, $userinput2); chomp($userinput1 = <STDIN>); #first name chomp($userinput2 = <STDIN>); #last name print "Hello ${userinput1}_$userinput2";
Re: Multiple Input help
by toolic (Bishop) on Feb 04, 2011 at 20:19 UTC
    Another way is to separate the variables:
    use warnings; use strict; my ($userinput1, $userinput2); $userinput1 = <STDIN>; #first name chomp $userinput1; $userinput2 = <STDIN>; #last name print "Hello $userinput1", '_', $userinput2;
    • I also fixed your typo: $userinputer2
    • You need to chomp to get rid of the newline
Re: Multiple Input help
by moritz (Cardinal) on Feb 04, 2011 at 20:23 UTC
Re: Multiple Input help
by chuckbutler (Prior) on Feb 04, 2011 at 20:31 UTC

    You can also escape the underscore by placing a backslash in front of it:

    . . . print "Hello $userinput1\_$userinput2"; . . .

    Good Luck. -c

Re: Multiple Input help
by starface245 (Novice) on Feb 10, 2011 at 16:17 UTC
    Thanks for all the input and using chomp works perfectly. Thanks again.
Re: Multiple Input help
by pavunkumar (Scribe) on Feb 05, 2011 at 05:51 UTC
     Try  This....  
    
    
    print "Hello $userinput1"."_"."$userinput2"; +

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://886286]
Approved by kennethk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2017-10-17 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (218 votes). Check out past polls.

    Notices?