I am
I am

Reputation: 1067

for each loop in Perl

I want to print each element in an array separating with tab. Here is the code I wrote:

print FILEOUT $_,"\t" foreach @data;

I have problem while printing. Extra tab is printing from beginning of the second line.

Can some one help me in this?

Upvotes: 0

Views: 437

Answers (2)

xiangpeis
xiangpeis

Reputation: 16

I think there is a "\n" at the end of each element of @array.

my @data = ("1\n", "2\n", "3\n");
print join "\t", map { chomp; $_; } @data;
print "\n";

Upvotes: 0

cjm
cjm

Reputation: 62099

Use join instead:

print FILEOUT join("\t", @data);

Upvotes: 6

Related Questions