What you mean to write is
print "$ref->[2][3]";
or
print "@$ref[2]->[3]";
From your description, I assume you've declared @Table
something like this:
my @Table = ([1, 2, 3, 4],
['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['i', 'j' 'k' 'l']);
That is, I'm pretty sure you left off my
since you aren't using use strict;
. How do I know this? You would have gotten a message saying Global symbol "@ref" requires explicit package name
if you had used it. What you're trying to access with $ref[2]
is an element in the array @ref
; not an element in the array ref $ref
. It's also possible that you used parens ((
and )
) to enclose the inner arrays instead of brackets ([
and ]
), which is a problem, because that would cause Perl to flatten the array into
my @Table = (1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' 'k' 'l');
which isn't what you want.
There are multiple problems with ${$ref[2][3]}
. First of all, the proper way to access elements within an array ref is $ref->[2]->[3]
, which can also be written as $ref->[2][3]
(I usually avoid that notation, since I think it's less intuitive). Had you succeeded in fetching that element, you would have wound up with ${"h"}
, which is a problem, because Perl complains that Can't use string ("h") as a SCALAR ref
.
EDIT: Since the question changed quite a bit after my answer, here's an applicable solution for the record:
#!/usr/bin/perl
use strict;
use warnings;
my $ref = [];
open (my $fh, "<", "file.txt") or die "Unable to open file $!\n";
push @$ref, [split] for (<$fh>);
close $fh;
print $ref->[1]->[2],"\n"; # print value at second row, third column
I saw this Perl references quick-reference posted in another answer on SO the other day. You would benefit from having a look at it. And never write Perl code without use strict;use warnings;
. That's asking for trouble.