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!