如何从存储在文件中的表中获取特定索引

发布于 2024-12-20 21:11:31 字数 356 浏览 2 评论 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”

请告诉我为什么输出没有出现

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2024-12-27 21:11:31

这是一个可以按照您的要求工作的代码:

# 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];

输出:

c

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
风渺 2024-12-27 21:11:31

你的意思是写的是

print "$ref->[2][3]";

或者

print "@$ref[2]->[3]";

根据你的描述,我假设你已经声明了类似这样的 @Table

my @Table = ([1, 2, 3, 4], 
     ['a', 'b', 'c', 'd'], 
     ['e', 'f', 'g', 'h'],
     ['i', 'j' 'k' 'l']);

也就是说,我很确定你离开了 my 因为您没有使用 use strict;。我怎么知道这个?如果您使用过的话,您会收到一条消息,指出全局符号“@ref”需要显式包名称。您尝试使用 $ref[2] 访问的是数组 @ref 中的一个元素;不是数组 ref $ref 中的元素。您也可能使用括号(())来括起内部数组,而不是方括号([]),这是一个问题,因为这会导致 Perl 将数组展平为

my @Table = (1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' 'k' 'l');

不是您想要的。

${$ref[2][3]} 存在多个问题。首先,访问数组ref中的元素的正确方法是$ref->[2]->[3],也可以写成$ref-> ;[2][3] (我通常避免使用这种符号,因为我认为它不太直观)。如果您成功获取该元素,您将得到 ${"h"},这是一个问题,因为 Perl 抱怨 Can't use string ("h")作为标量引用

编辑:由于问题在我回答后发生了很大变化,因此记录了一个适用的解决方案:

#!/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

我看到了这个Perl 引用快速参考 前几天在 SO 的另一个答案中发布。看看它你就会受益匪浅。如果没有 use strict;use warnings;,切勿编写 Perl 代码。那是自找麻烦。

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.

明明#如月 2024-12-27 21:11:31

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

Perl 是一种零索引语言,就像 C 和 Java 以及任何其他语言。如果您希望$table->[2][3]为'c',则需要以某种方式分配它。

另外,简单地创建一个行数组是行不通的。 @Table =; 只是创建一个包含四行的一维数组。您至少需要这样做:

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

但是,这仍然无法解决索引问题。但这会:

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

建议设置$[< /代码>

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 $[!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文