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

hi, how can i print this in one line? right now, the numbers and answers print on new lines
#!usr/bin/perl print "Enter your first number:\n "; $number=<STDIN>; print "Enter your second number:\n "; $number2=<STDIN>; $sum=$number + $number2 ; $product=$number * $number2 ; $quotient=$number / $number2 ; print "Hello! The sum of $number and $number2 is $sum." ; print "The product of $number and $number2 is $product." ; print " When $number is divided by $number2, the quotient is $quotient +." ;
thank you

Replies are listed 'Best First'.
Re: output question
by choroba (Cardinal) on Jun 27, 2014 at 12:41 UTC
    By omitting the <code> tags, you put your question in one line. See Markup in the Monastery for details.

    When the user enters a number, he finishes with an Enter. This Enter is part of the $number, you can remove it with chomp.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: output question
by AppleFritter (Vicar) on Jun 27, 2014 at 16:17 UTC

    Hello enderw2014, welcome to the monastery!

    As choroba already said, the angle bracket operator returns everything that was read, including the final newline. Use chomp, or alternatively chop to remove it (the former, essentially being a smarter version of the latter, is usually the better choice). You can even chomp a variable at the same time as assigning to it:

    chomp($number = <STDIN>);

    A few other tips, since I imagine you're just getting started with Perl:

    • Format your code to look nice. It may not matter for throwaway scripts you only run once to get a specific task done, but for anything you'll be coming back to later, it really pays off.
    • use strict;. This will catch all sorts of issues that might come back to bite you later on. use warnings; may also be worth considering.
    • Depending on the Perl version(s) you're targetting, enable modern features and make use of them, e.g. say.
    • Also, just for the future: if your script produces errors or warnings you don't understand, use diagnostics; to get long-form explanations of what's wrong.

    Have fun with Perl!

Re: output question
by Anonymous Monk on Jun 28, 2014 at 10:42 UTC

    Your input includes the "enter" from the keyboard. You need to chomp your input. For example:

    print "Enter your first number:\n "; $number=<STDIN>;

    Needs to become something like:

    print "Enter your first number:\n "; $number=<STDIN>; chomp $number;