CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unwanted new line

 



AlphaScorpious
Novice

Sep 22, 2013, 6:54 PM

Post #1 of 3 (520 views)
Unwanted new line Can't Post

Hello! I just started learning PERL this weekend. I am using Strawberry PERL 64-bit with Windows 7 and currently going through the free book Beginning PERL.

I currently have this simple script that converts from Dollars to Euros:


Code
#!/usr/bin/perl 
use warnings;
use strict;
print "\n ";
print "************************\n ";
print "** CURRENCY CONVERTER **\n ";
print "************************\n\n\n ";
print "To convert US Dollars to Euros, please enter the current exchange rate:\n\n ";
my $EuroExchangeRate = <STDIN>;
print "\n ";
print "Enter a US Dollar amount to convert:\n\n ";
my $InputDollar = <STDIN>;
print "\n ";
my $Euros = $EuroExchangeRate * $InputDollar;
print "\$", $InputDollar, " is equal to ", $Euros, " Euros\n";


If you run the code, the script displays with 2 lines of text instead of one, with the value of $InputDollar ending a line and "is equal to " beginning a new one. Can someone please explain why?


Zhris
Enthusiast

Sep 22, 2013, 7:32 PM

Post #2 of 3 (518 views)
Re: [AlphaScorpious] Unwanted new line [In reply to] Can't Post

Hi,

Whenever you read from standard input, you should remove the newline character appended onto the end of the returned string, where the user hit return:


Code
my $EuroExchangeRate = <STDIN>;  



chomp(my $EuroExchangeRate = <STDIN>);

# or

my $EuroExchangeRate = <STDIN>;
chomp $EuroExchangeRate;



Chris


(This post was edited by Zhris on Sep 22, 2013, 7:35 PM)


AlphaScorpious
Novice

Sep 22, 2013, 9:07 PM

Post #3 of 3 (507 views)
Re: [Zhris] Unwanted new line [In reply to] Can't Post

Thank you very much!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives