How to print a Perl 2-dimensional array?

Posted by Matt Pascoe on Stack Overflow See other posts from Stack Overflow or by Matt Pascoe
Published on 2010-06-10T21:36:44Z Indexed on 2010/06/10 21:42 UTC
Read the original article Hit count: 389

Filed under:
|

I am trying to write a simple Perl script that reads a *.csv, places the rows of the *.csv file in a two dimensional array, and then prints on item out of the array and then prints a row of the array.

#!/usr/bin/perl
use strict;
use warnings;

open(CSV, $ARGV[0]) || die("Cannot open the $ARGV[0] file: $!");
my @row;
my @table;

while(<CSV>) {
        @row = split(/\s*,\s*/, $_);
        push(@table, @row);
}
close CSV || die $!;

foreach my $element ( @{ $table[0] } ) {
    print $element, "\n";
}

print "$table[0][1]\n";

When I run this script I receive the following error and nothing prints:

Can't use string ("1") as an ARRAY ref while "strict refs" in use at ./scripts.pl line 16.

I have looked in a number of other forums and am still not sure how to fix this issue. Can anyone help me fix this script?

© Stack Overflow or respective owner

Related posts about perl

Related posts about array