LinuxQuestions.org
Review your favorite Linux distribution.
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 01-11-2010, 11:01 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
Why does string concatenation in perl result in two lines, not one?


Hello,

I am new to perl and am having trouble adding some strings together. My full code is below:

#!/usr/bin/perl

open FinalFileList,"<","finalFileList.txt" || die "could not open file";

open (linkCommands, '>makeLinks.sh');

$myNum = 8;

while (<FinalFileList>){
$NewCommand = "ln -s " . $_;
$NewCommandB = $NewCommand . "/atlas2/hodgkinson/REALDATA_Virtual/data09_900GeV.00141707.physics_MinBias.merge.DESD_COLLCAND.f183_m300/data09_900GeV.00142383.physics_MinBias.merge.DESD_COLLCAND.f190_m311_";
#. $myNum . ".pool.root";
print linkCommands "$NewCommandB\n";
$myNum += 1;
}

close(FinalFileList);
close(linkCommands);

The problem is $NewCommandB is always split into two lines, where the second line contains the "/atlas2/<blah>/<etc>/..." string. Since I am generating a .sh file to execute a lot of similar commands I need the string to all be on one line. Any idea why I get this behaviour and any suggestion on how to tell perl to make $NewCommandB a one line string?

btw for completeness finalFileList.txt contains just file names one line after another:

data09_900GeV.00142383.physics_MinBias.merge.DESD_COLLCAND.f190_m311._lb0251.1
data09_900GeV.00142383.physics_MinBias.merge.DESD_COLLCAND.f190_m311._lb0252.1
etc etc

Thanks!,

Mark
 
Old 01-11-2010, 11:19 AM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
You are including the newline from the end of the line in the file. Use the chomp() function around the variable to remove it.
 
1 members found this post helpful.
Old 01-11-2010, 11:52 AM   #3
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by devnull10 View Post
You are including the newline from the end of the line in the file. Use the chomp() function around the variable to remove it.
Thanks for the help - adding:

chomp $_;

at the start if my while loop does the trick

Cheers,

Mark
 
Old 01-12-2010, 02:49 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Always start with
Code:
#!/usr/bin/perl -w
use strict;
It'll save you no end of grief, I promise.
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
Old 01-12-2010, 03:07 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by markhod View Post
Thanks for the help - adding:

chomp $_;

at the start if my while loop does the trick

Cheers,

Mark
It's cosmetic, but you may even write
Code:
chomp;
instead of

Code:
chomp $_;
.

Better yet, do not rely on $_ unless in one liners, 'grep', 'map'.

And, as said many times and just above -always use

Code:
use strict;
in your code.
 
Old 01-12-2010, 09:00 PM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,425

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
The best prologue I have found is:
Code:
use strict; 
use warnings;
Perl is an extremely robust and full-featured language, especially when the vast CPAN library is considered and properly brought to bear. As a perfectly general blanket statement: always start your program design with a thorough review of what CPAN already offers you.

Last edited by sundialsvcs; 01-12-2010 at 09:01 PM.
 
  


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
String concatenation problem in a loop tugce_zehra Linux - Newbie 1 12-06-2008 06:43 PM
Shell string concatenation Quakeboy02 Programming 4 04-26-2007 12:11 PM
string concatenation in c/c++ for a socket program mohtasham1983 Programming 3 02-14-2007 06:42 PM
string concatenation in AWK xanthium Programming 1 04-22-2002 05:41 AM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration