Reputation: 157
I am taking the output from a file in tabular form.
This is how the file.txt will look like. suppose its a 4*4 matrix
1 2 3 4
a b c d
e f g h
i j k l
Now i want to fetch a particular element of the table say 2nd row and 3rd column. I am using the below code but i am not getting the ouput.
I am storing the table in a array and taking the ref of it.
open(FH, "file.txt);
@Table = <FH>;
close FH;
$ref = \@Table;
print "${$ref[2][3]}";
The ouput should be "c"
Please tell me why ouput is not coming
Upvotes: 0
Views: 165
Reputation: 7579
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.
Upvotes: 2
Reputation: 29854
No, it shouldn't be 'c'. Not if you want the 3rd row ( indexes: 0, 1, 2 ) and 4th column (indexes: 0, 1, 2, 3 ).
Perl is a zero-index language, like C and Java and any number other languages. If you want $table->[2][3]
to be 'c', you need to assign it in a certain way.
Also, simply making an array of the lines is not going to work. @Table = <FH>;
just creates a single-dimensional array with the four lines in it. You would need to do at least this:
@Table = map { [ split ' ' ] } <FH>;
However, that's still not going to fix the index issue. But this will:
@Table = ( undef, map { [ undef, split ' ' ] } <FH> );
I do not recommend setting $[
!!
Upvotes: 1
Reputation: 91430
Here is a code that works as you want:
# ALWAYS use these 2 lines at the begining of your programs
use strict;
use warnings;
my $file = 'file.txt';
# use lexical file handler, 3 arg open and test if open is OK
open my $fh, '<', $file or die "unable to open '$file' for reading:$!";
my @Table;
while(<$fh>) {
push @Table,[split];
}
close $fh;
my $ref = \@Table;
# this prints the third element of the second line
# array index start at 0
print $ref->[1][2];
output:
c
Upvotes: 2