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

假设我有一个我想要处理的数据文件;我想获取每列的最大值并将其附加到每行的末尾.

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.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...