Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Output split over two lines

by thevoid (Scribe)
on Dec 23, 2006 at 17:38 UTC ( [id://591457]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I'm new to programming and have started on a "Beginning Perl" tutorial. I'm having a few problems and am hoping someone here can shed some light :) One of the first exercises was to write a program for a basic currency converter, here's what I did -
#!/usr/bin/perl -w #currency2.plx use warnings; use strict; print "Currency converter\n\nPlease enter the exchange rate: "; my $yen = <STDIN>; print "Now please enter a price to convert\n"; print "Price:"; my $price = <STDIN>; print "$price Yen is ", ($price/$yen), "pounds\n";
The problem is that when it prints the answer (line 10) it splits it over two lines, with the amount $price on the first line and rest - Yen is... - on the next. Why is this? At this stage I'm just using Windows notepad and running the program in the command prompt as I haven't gotten as far as using a proper text editor yet.

Replies are listed 'Best First'.
Re: Output split over two lines
by shmem (Chancellor) on Dec 23, 2006 at 17:46 UTC
    perl is doing what you told it to do:
    my $yen = <STDIN>;

    Now the "\r?\n" is still attached to the contents of the variable $yen. You want to say

    chomp(my $yen = <STDIN>);

    See chomp and chop.

    <update>

    See also perlvar for $/.

    </update>

    --shmem

    update: changed "\n" to "\r?\n" above, and /s/chop/chomp/, since chop chops one character, while chomp removes a trailing $/ (which might be "\r\n" or "\r". Thanks to ikegami for telling me in a /msg.

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      nice, cheers
Re: Output split over two lines
by idsfa (Vicar) on Dec 23, 2006 at 17:47 UTC

    The variable $price contains all of your input, which includes the new line (Enter). When you print it out as a string in the last line, the new line is printed as well. This doesn't happen later in that line when the price in pounds is printed because you are printing the result of an arithmetic calculation, and the newline is silently dropped (from both $price and $yen) when they are treated as numbers instead of strings ... no they don't cancel out ;-).

    There is a command in perl specifically for stripping the Enter key results from your input: chomp. Although it is related to chop, which shmem suggested, it is superior for this purpose because it will work on different platforms (Unix, Windows, Mac OS, etc). chop will not give you what you want in Windows (for example), where the enter key appends two characters.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: Output split over two lines
by ikegami (Patriarch) on Dec 23, 2006 at 17:47 UTC

    $yen and $prince contain the newline from pressing Enter. Use

    chomp( my $yen = <STDIN> );

    and

    chomp( my $price = <STDIN> );

    to remove any trailing newline.

    Ref: chomp

Re: Output split over two lines
by thevoid (Scribe) on Dec 23, 2006 at 17:46 UTC
    ooops, forgot to say, thanks for any help!!
Re: Output split over two lines
by thevoid (Scribe) on Dec 23, 2006 at 17:50 UTC
    brilliant, thanks all for your help ; )
Re: Output split over two lines
by ysth (Canon) on Dec 24, 2006 at 10:44 UTC
    At this stage I'm just using Windows notepad and running the program in the command prompt as I haven't gotten as far as using a proper text editor yet.
    I don't know how well this might work for a newbie (and in fact am interested in hearing whether or not it does), but you might get a trial version of Komodo from ActiveState to use while you are learning the basics.
Re: Output split over two lines
by wjw (Priest) on Dec 27, 2006 at 03:52 UTC
    As long as your at the command line ( a good place to start IMHO), fire your perl script off using the built in debugger...  perl -d (your_program.pl).

    It gives you a quick an dirty way of seeing what you are getting without having to glop your program up with print statements. The debugger is very easy to use, even if you only know a couple of things about it. You can find out how to use it pretty quick here -> perldebug

    You will hear some snear and some chuckle about using a debugger, but it is a darn handy tool for learning about what is happening in your code... .

    Good Luck!

    ...the majority is always wrong, and always the last to know about it...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found