加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Perl单行中调用模块?

发布时间:2020-12-15 21:52:06 所属栏目:大数据 来源:网络整理
导读:假设我有一个我想要处理的数据文件;我想获取每列的最大值并将其附加到每行的末尾. INPUT: T1 T2 T335.82 34.67 31.6832.20 34.52 33.5937.41 38.64 37.56 OUTPUT: T1 T2 T335.82 34.67 31.68 35.8232.20 34.52 33.59 34.5237.41 38.64 37.56 38.64 我试图
假设我有一个我想要处理的数据文件;我想获取每列的最大值并将其附加到每行的末尾.

INPUT:

T1 T2 T3
35.82 34.67 31.68
32.20 34.52 33.59
37.41 38.64 37.56

OUTPUT:

T1 T2 T3
35.82 34.67 31.68 35.82
32.20 34.52 33.59 34.52
37.41 38.64 37.56 38.64

我试图将其作为一个单行程来实现.到目前为止,这是我提出的,虽然它抱怨& main :: max未定义:

perl -MList::Util -ani.bak -e "print qq(@F).q( ).max(@F).qq(n)" file1.txt

好像我还没有加载List :: Util模块.怎么了?标题栏是一个问题吗?

perlrun没有关于如何做到这一点的一个很好的例子(实际上它确实如此,我的文档有点难以阅读).

解决方法

您加载了List :: Util,但默认情况下模块不导出符号.通过检查是否跳过标题行.是1.
$perl -MList::Util=max -ape 's/$/" " . max(@F)/e unless $.==1' input 
T1 T2 T3
35.82 34.67 31.68 35.82
32.20 34.52 33.59 34.52
37.41 38.64 37.56 38.64

perlrun文档说明:

A little builtin syntactic sugar means you can also say -mmodule=foo,bar or -Mmodule=foo,bar as a shortcut for -Mmodule qw(foo bar). This avoids the need to use quotes when importing symbols. The actual code generated by -Mmodule=foo,bar is use module split(/,/,q{foo,bar}). Note that the = form removes the distinction between -m and -M.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读