Reputation: 77
I am trying to concatenate 3 strings in perl, and I am getting weird behavior. The data was just written to a file previously in the script, and I am trying to add two columns to the data.
Here is my code and its behavior
print "phylipId is $phylipId\n";
print "Tree is $tree\n";
print "Line is $line\n";
my $string = join "\t", $phylipId, $tree, $line;
print "Concatenated is $string\n";
Gives me this output
phylipId is 4
Tree is (138,((139,141),140));
Line is 000931 17.0 1.0 0.135 no 1044 646918204
Concaten000931s 17.0 1.08,((10.1351),no0)); 1044 646918204
This also happened when I used the . operator. Any help would be appreciated
Upvotes: 2
Views: 1315
Reputation: 126722
As patrick says, it is more than likely you have read a DOS-formatted file on a Linux box. In those circumstances, if you use chomp
on a string terminated with "\r\n"
you will be left with the "\r"
.
The simplest way to clean up records like this is to replace chomp
with
s/\s+$//
which, since both "\r"
and "\n"
count as whitespace, will remove both from the string simultaneously. If trailing tabs and spaces are important for you then use
s/[\r\n]+$//
instead, or perhaps
s/[[:cntrl:]]+$//
or
s/\p{Control}+$//
Upvotes: 1
Reputation: 8807
It looks like youre reading $tree
from a file using carriage-returns (\r
), and $tree
is ending up with \r
at the end of it causing it to seek to the beginning of the line.
See this test:
perl -e 'print("abcdefghijkl\r\t012\n");'
Which outputs
abcdefgh012l
Upvotes: 4
Reputation: 16786
I'm not able to replicate your issue after trying under Windows and Linux.
Upvotes: 0