0

我正在以表格形式从文件中获取输出。

这就是 file.txt 的样子。假设它是一个 4*4 矩阵

1 2 3 4
a b c d
e f g h
i j k l

现在我想获取表格的特定元素,比如第二行和第三列。我正在使用下面的代码,但我没有得到输出。

我将表格存储在一个数组中并获取它的引用。

open(FH, "file.txt);
@Table = <FH>;
close FH;
$ref = \@Table;
print "${$ref[2][3]}";

输出应该是“c”

请告诉我为什么没有输出

4

3 回答 3

2

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
于 2011-12-12T09:14:32.940 回答
2

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.

于 2011-12-12T08:54:16.953 回答
1

不,它不应该是“c”。如果您想要第3行(索引:0、1、2)和第 4列(索引:0、1、2、3),则不是。

Perl 是一种零索引语言,就像 C 和 Java 以及任何数量的其他语言一样。如果你想$table->[2][3]成为'c',你需要以某种方式分配它。

此外,简单地制作线条数组是行不通的。@Table = <FH>;只需创建一个包含四行的一维数组。您至少需要这样做:

@Table = map { [ split ' ' ] } <FH>;

但是,这仍然不能解决索引问题。但这将:

@Table = ( undef, map { [ undef, split ' ' ] } <FH> );

建议设置$[

于 2011-12-12T13:18:37.603 回答