It's hard to tell whether you meant it or not, but the reason why you're getting array references is because you're not printing what you think you are.
You started out right when iterating over the 'rows' of @data
with:
foreach my $line (@data) { ... }
However, the next line is a no-go. It seems that you're confusing text strings with an array structure. Yes, each row contains strings, but Perl treats @data
as an array, not a string.
split
is used to convert strings to arrays. It doesn't operate on arrays! Same goes for chomp
(with an irrelevant exception).
What you'll want to do is replace the contents of the foreach
loop with the following:
foreach my $line (@data) {
print $line->[0].", ".$line->[1].", ".$line->[0]."\n";
}
You'll notice the ->
notation, which is there for a reason. $line
refers to an array. It is not an array itself. The ->
arrows deference the array, allowing you access to individual elements of the array referenced by $line
.
If you're not comfortable with the idea of deferencing with arrows (and most beginners usually aren't), you can create a temporary array as shown below and use that instead.
foreach my $line (@data) {
my @random = @{ $line };
print $random[0].", ".$random[1].", ".$random[2]."\n";
}
OUTPUT
1_TEST, 1_T, 1_TESTER
2_TEST, 2_T, 2_TESTER
3_TEST, 3_T, 3_TESTER
4_TEST, 4_T, 4_TESTER
5_TEST, 5_T, 5_TESTER
6_TEST, 6_T, ^_TESTER
A one-liner might go something like map { print "@$_\n" } @data;
(which is a bit OTT), but if you want to just print the array to see what it looks like (say, for debugging purposes), I'd recommend using the Data::Dump
module, which pretty-prints arrays and hashes for you without you having to worry about it too much.
Just put use Data::Dump 'dump';
at beginning of your script, and then dump @data;
. As simple as that!