How can I name a module in single-line Perl?

Say I have a data file that I want to process; I want to take the maximum value of each column and add it to the end of each row.

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 

I am trying to implement this as a single line. So far this is what I came up with, although it complains that &main::max is undefined:

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

It seems that I did not load the List::Util module. What's wrong? And the problem with the column header?

perlrun has no decent example on how to do this (actually it is, my documentation was a bit hard to read).

+7
module perl
source share
4 answers

List::Util loaded, but the module does not export characters by default. Skip the title bar, checking to see if $. equal to 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 

The perlrun documentation explains:

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

+19
source share
 perl -M"List::Util 'max'" -ani.bak -e "print qq(@F).q( ).max(@F).qq(\n)" file1.txt 
+4
source share

List :: Util was loaded, but it does not export the max function by default:

 perl -MList::Util -ani.bak -e "print qq(@F).q( ).List::Util::max(@F).qq(\n)" file1.txt 
+2
source share

if Perl is optional, here is awk one-liner

 $ awk '{for(i=1;i<=NF;i++)if($i>t){t=$i};print $0,t;t=0}' file 35.82 34.67 31.68 35.82 32.20 34.52 33.59 34.52 37.41 38.64 37.56 38.64 
+1
source share

All Articles