LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why does string concatenation in perl result in two lines, not one? (https://www.linuxquestions.org/questions/programming-9/why-does-string-concatenation-in-perl-result-in-two-lines-not-one-781482/)

markhod 01-11-2010 10:01 AM

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

devnull10 01-11-2010 10:19 AM

You are including the newline from the end of the line in the file. Use the chomp() function around the variable to remove it.

markhod 01-11-2010 10:52 AM

Quote:

Originally Posted by devnull10 (Post 3822567)
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

chrism01 01-12-2010 01:49 AM

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

Sergei Steshenko 01-12-2010 02:07 AM

Quote:

Originally Posted by markhod (Post 3822607)
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.

sundialsvcs 01-12-2010 08:00 PM

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.


All times are GMT -5. The time now is 10:17 AM.