Reputation: 409
I'm having trouble using the join() in perl. I want to join a single column to the last column in each line in an array (tab delimited). For example (assume tab delimited):
@array =
blue black grey
red orange pink
@add =
hello
goodbye
desired output =
blue black grey hello
red orange pink goodbye
The code I have is:
foreach my $line (@array) {
my $line2 = join ("\t", $line, $add[$1]);
print $line2;
$i++;
}
However, what I get is:
blue black grey
hellored orange pink
I seem to be appending the first entry of @add (hello) to BEFORE the second line of where i want it to be. Any ideas?!
This may help:
my $i = 0;
foreach my $line (@array) {
print $line;
$i++;
}
output:
blue black grey
red orange pink
So my array is fine. If I print out $add[$1]:
my $i = 0;
foreach my $line (@array) {
print $add[$i];
$i++;
}
Output: hellogoodbye
Hope that helps. I'm sure it's something simple. I thought maybe a chomp issue, but no luck. E
Upvotes: 0
Views: 289
Reputation: 385789
The elements of @array
end in line feeds. Use chomp
to remove them.
You probably have something like
my @array = <>;
Change that to
chomp( my @array = <> );
Don't forget to add a line feed when you output! (Or use say
instead of print
.)
Upvotes: 2