Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: outputs followed by each other

by thanos1983 (Parson)
on Feb 16, 2018 at 09:41 UTC ( [id://1209279]=note: print w/replies, xml ) Need Help??


in reply to outputs followed by each other

Hello busyvish,

Welcome to the Monastery. Fellow Monks has already answered your question, but since you said that you are a beginner I would like to add something minor here that I think it will help you in future also. Always always use strict and warnings on your scripts. It is not really matter if you experienced developer or not but including those two lines they will guide you in avoiding many many mistakes.

Having said that, sample of code included all the recommendations:

#!/usr/bin/perl use strict; use warnings; print "Please enter the first number: "; chomp (my $n1=<>); print "Please enter the second number: "; chomp (my $n2=<>); if ($n1 < $n2) { print "Output: $n1 $n2\n"; } else { print "Output: $n2 $n1\n"; } __END__ $ perl test.pl Please enter the first number: 4 Please enter the second number: 5 Output: 4 5
Update: Well I was bothered for a minute and I was thinking that your simple script will fail in case of non numerical characters. Your script can not handle this exception in case the user enters this type of characters as an input. So given this in consideration I used a simple perlre (read more on the link) and also a die function to capture this type of characters and exit in case that it matches.

Sample of code:

#!/usr/bin/perl use strict; use warnings; print "Please enter the first number: "; chomp (my $n1=<>); die "Please enter numerical(s) characters only not '$n1'!" unless $n1 =~ /^\d+$/; print "Please enter the second number: "; chomp (my $n2=<>); die "Please enter numerical(s) characters only not '$n2'!" unless $n2 =~ /^\d+$/; if ($n1 < $n2) { print "Output: $n1 $n2\n"; } else { print "Output: $n2 $n1\n"; } __END__ $ perl test.pl Please enter the first number: Test Please enter numerical(s) characters only not 'Test'! at test.pl line +8, <> line 1.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found