LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2016, 11:23 AM   #1
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 259

Rep: Reputation: 24
Perl, Interpolated Variable Cause a Newline in Output


Here is my, admittedly simple, code:
Code:
#!/usr/bin/perl
print "Enter a number: ";
$num1 = <STDIN>;

print "Enter another number: ";
$num2 = <STDIN>;

print "The product of $num1 and $num2 is: ", $num1 * $num2, ".\n";
Here is the output:
Code:
Enter a number: 12
Enter another number: 12
The product of 12
 and 12
 is: 144.
I've searched, but no answers are jumping off the screen at me.

Thank you for any help.

PS. Any other vim users ever try :wq to submit a post? Note: It doesn't work.

Last edited by killingthemonkey; 08-30-2016 at 11:25 AM.
 
Old 08-30-2016, 11:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
When you get the number from STDIN, how does the user advise the program when they have finished entering the number they want?
 
Old 08-30-2016, 12:12 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You need to chomp your variables...
Code:
$num1 = <STDIN>;
chomp $num1;
...
 
Old 08-30-2016, 12:16 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,633
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
To clarify, the line of input you are getting from <STDIN> includes a newline character at the end of the string.

chomp (or chop in PHP) removes this character from the string.

You are seeing newlines in your printed output because the strings that you are printing contain newline characters.
 
Old 08-30-2016, 08:22 PM   #5
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 259

Original Poster
Rep: Reputation: 24
Talking

Thank you!

chomp was the solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert a newline after output command micronemo Linux - Newbie 1 09-13-2012 10:16 AM
[SOLVED] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 02:35 PM
replace space with newline and save result in a variable sktimmy Linux - General 5 11-01-2011 11:56 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
perl: send output of system() call to variable drkstr Programming 4 07-23-2006 10:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration