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

Re: Question why my code is posting a new line

by Athanasius (Chancellor)
on Dec 18, 2012 at 02:35 UTC ( #1009267=note: print w/replies, xml ) Need Help??

in reply to Question why my code is posting a new line

Hello chiggly007, and welcome to the Monastery!

When the user types (say) “1.5” and presses “Enter”, $price is assigned “1.5\n” (note the newline). Then when print "$price" is called, this newline is printed to the screen.

Solution: chomp the input:

my $price = <STDIN>; chomp $price; print "$price Yens ", ($price / $yen), " pounds\n";

(Also, please fix the <code> / </code> tags in your post so that your script prints clearly. See Markup in the Monastery.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Question why my code is posting a new line
by chiggly007 (Initiate) on Dec 18, 2012 at 03:05 UTC
    Thanks for the help Athanasius. So are you saying that assigning a variable, and hitting Enter , Perl automatically assigns a "\n" to that variable? Thanks

      No. Athanasius is implying that:

      my $price = <STDIN>;

      reads an entire line including the end of line character(s) into $price. Perl doesn't "add" the new line, it was there in the input. chomp may be used to remove a trailing line end.

      Most often the line end is represented in Perl strings as \n regardless of operating system. In various cases is can be other strings (for example when the content of $/ has been changed).

      True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009267]
[choroba]: Good morning, if a bit rainy
[marto]: life is a struggle :P I'm glad the GPW went well.
[Corion]: Hi choroba! Yeah, I brought the rain with me from Hamburg
[Corion]: marto: As am I ;) And we got two interested locations for next year, Munich and Cologne maybe, which is great as well. Not Berlin though ;)
[marto]: damn

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-06-29 08:10 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (655 votes). Check out past polls.