如何从存储在文件中的表中获取特定索引
我正在以表格形式从文件中获取输出。
这就是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个可以按照您的要求工作的代码:
输出:
Here is a code that works as you want:
output:
你的意思是写的是
或者
根据你的描述,我假设你已经声明了类似这样的
@Table
:也就是说,我很确定你离开了
my
因为您没有使用use strict;
。我怎么知道这个?如果您使用过的话,您会收到一条消息,指出全局符号“@ref”需要显式包名称
。您尝试使用$ref[2]
访问的是数组@ref
中的一个元素;不是数组 ref$ref
中的元素。您也可能使用括号((
和)
)来括起内部数组,而不是方括号([
和]),这是一个问题,因为这会导致 Perl 将数组展平为
不是您想要的。
${$ref[2][3]}
存在多个问题。首先,访问数组ref中的元素的正确方法是$ref->[2]->[3]
,也可以写成$ref-> ;[2][3]
(我通常避免使用这种符号,因为我认为它不太直观)。如果您成功获取该元素,您将得到${"h"}
,这是一个问题,因为 Perl 抱怨Can't use string ("h")作为标量引用
。编辑:由于问题在我回答后发生了很大变化,因此记录了一个适用的解决方案:
我看到了这个Perl 引用快速参考 前几天在 SO 的另一个答案中发布。看看它你就会受益匪浅。如果没有
use strict;use warnings;
,切勿编写 Perl 代码。那是自找麻烦。What you mean to write is
or
From your description, I assume you've declared
@Table
something like this:That is, I'm pretty sure you left off
my
since you aren't usinguse strict;
. How do I know this? You would have gotten a message sayingGlobal 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 intowhich 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 thatCan'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:
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.不,它不应该是“c”。如果您想要第 3 行(索引:0, 1, 2)和第 4 列(索引:0, 1, 2, 3),则不需要。
Perl 是一种零索引语言,就像 C 和 Java 以及任何其他语言。如果您希望
$table->[2][3]
为'c',则需要以某种方式分配它。另外,简单地创建一个行数组是行不通的。
@Table =;
只是创建一个包含四行的一维数组。您至少需要这样做:但是,这仍然无法解决索引问题。但这会:
我不建议设置
$[< /代码>
!
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:However, that's still not going to fix the index issue. But this will:
I do not recommend setting
$[
!!