busyvish has asked for the wisdom of the Perl Monks concerning the following question:

hello guys before i start i am a complete newbie to perl. i have a code that runs just about fine. the problem is with the output. this is the code
#! /usr/bin/perl -l print "please enter a number: "; $n1 = <STDIN>; print "please enter a number"; $n2 = <STDIN>; if ($n1 < $n2) { print "$n1" , "$n2" ; } else { print "$n2" , "$n1" ; }
i need the output in a specific format $n1 followed by $n2 or vice versa this code gives me values in format $n1 newline $n2 what can i do to solve my problem

Replies are listed 'Best First'.
Re: outputs followed by each other
by 1nickt (Canon) on Feb 16, 2018 at 00:26 UTC

    Hi, the value of your variables includes the new line character that was added when the user hit 'Enter'.

    It's a good idea to examine variable values enclosed in delimiters so you can spot any invisible characters, like this: print ">$var<";

    You can remove the newline character with chomp.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: outputs followed by each other
by NetWallah (Canon) on Feb 16, 2018 at 06:05 UTC
    A more experienced programmer would get input like this:
    chomp (my $n1=<>); # STDIN is the default
    and print output like:
    print "$n1,$n2;\n";

                    Python is a racist language what with it's dependence on white space!

Re: outputs followed by each other
by thanos1983 (Parson) on Feb 16, 2018 at 09:41 UTC

    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:

    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:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: outputs followed by each other
by BillKSmith (Monsignor) on Feb 17, 2018 at 16:48 UTC
    Even on simple programs like this, CPAN modules can be a big help. In the following example, the modules require little additional code, but greatly extend the capability. The IO::Prompt::Hooked not only takes care of the newline problem, it validates the input, and gives the user another chance if it is not valid. (There are more options available that you may wish to experiment with.) Regexp::Common extends the validation to allow any valid real number format.
    #! /usr/bin/perl -l use strict; use warnings; use IO::Prompt::Hooked; use Regexp::Common; my %opt = ( message => "please enter a number: ", validate => qr/^$RE{num}{real}$/, error => "Input must be a number\n", ); my $n1 = prompt(%opt); my $n2 = prompt(%opt); if ($n1 <= $n2) { print "$n1 <= $n2\n" ; } else { print "$n2 <= $n1\n" ; }
    Bill
Re: outputs followed by each other
by Anonymous Monk on Feb 16, 2018 at 14:52 UTC
    If you accept input and do not them use chomp on that input, then the input will contain a newline character. The example in the hyperlink tells all.