gpt4 book ai didi

perl - 如何在 Perl 中打印/提取二维数组列下的信息?

转载 作者:行者123 更新时间:2023-12-04 21:05:19 25 4
gpt4 key购买 nike

我有一个输出文件,它是一个二维数组(这个文件是在运行为生成二维数组而编写的脚本后生成的输出),我必须读取特定列下的信息,比如第 1 列。换句话说,如何我阅读并打印出列在第 1 列下与所有行相对应的信息。

有什么建议吗?

__DATA__
1 2 3 4 5 6 7 8 9
A B C D E F G H I
93 48 57 66 52 74 33 22 91

从上面的数据中,我想按列提取信息,比如说,如果我想从第 1 列中获取信息,我应该只能列出以下输出。想要列出然后我想要

输出:

1
A
93

最佳答案

所有修正后的最终版本:

#!/usr/bin/perl

use strict;
use warnings;

my $column_to_show = 0;

while ( <DATA> ) {
last unless /\S/;
print +(split)[$column_to_show], "\n";
}

__DATA__
1 2 3 4 5 6 7 8 9
A B C D E F G H I
93 48 57 66 52 74 33 22 91

输出:

C:\Temp> u
1
A
93

print +(split)[$column_to_show], "\n";的解释:

perldoc -f split :

Splits the string EXPR into a list of strings and returns that list. ... If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on whitespace (after skipping any leading whitespace).

所以:(split)[3]选择 split 返回的列表的第四个元素. +(split) 前有必要帮忙perl正确解析表达式。参见 perldoc -f print :

Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print — interpose a + or put parentheses around all the arguments.

我强烈建议每个 Perl 程序员偶尔浏览一下所有文档 perldoc perltoc .它在您的计算机上。

关于perl - 如何在 Perl 中打印/提取二维数组列下的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com