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 |
You are including the newline from the end of the line in the file. Use the chomp() function around the variable to remove it.
|
Quote:
chomp $_; at the start if my while loop does the trick :) Cheers, Mark |
Always start with
Code:
#!/usr/bin/perl -w http://perldoc.perl.org/ http://www.perlmonks.org/?node=Tutorials |
Quote:
Code:
chomp; 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; |
The best prologue I have found is:
Code:
use strict; |
All times are GMT -5. The time now is 10:17 AM. |