Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Print the line with the largest number from standard input

by mr_ron (Chaplain)
on Aug 05, 2019 at 02:54 UTC ( [id://11103902]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Print the line with the largest number from standard input
in thread Print the line with the largest number from standard input

Your refactoring improvement gave me some (2) ideas for command line / one liner style solutions. In the process of researching, however, I rechecked StackOverflow and noticed choroba's answer which is elegant and could be easily rewritten as a command line similar to my examples. I am not sure the problem from the OP really needs that much more research, but will give my results here as they may have an idea of interest to somebody and are hopefully short enough that reading them will not take too long.

My refactored code is included below but, again, given the StackOverflow solution, my second example, which takes a different approach, may be more interesting.

#!/usr/bin/env sh perl -Mstrict -MList::Util=max -MRegexp::Common -Mv5.10 -wne ' our ($f_max, $f_max_line); defined( my $l_max = max /$RE{num}{real}/g ) or next LINE; ($f_max, $f_max_line) = ($l_max, $_) if not defined $f_max or $l_max > $f_max; END { print $f_max_line // "No line with number found.\n" } '

The sort command in my second and last example might become a performance concern for large files, but it's almost all off the shelf with very little custom code.

#!/usr/bin/env sh perl -Mstrict -MList::Util=max -MRegexp::Common -Mv5.10 \ -wne 'print max(/$RE{num}{real}/g) // "NaN", " : ", $_' | sort -k1nr | head -1 | sed 's/^NaN :.*$/No line with number found./;s/^[0-9-]* : //'

Anyone have a good argument for preferring

-Mstrict -Mv5.10
against
-Mv5.12
??

The code was tested on macOS Mojave/perl 5.18 and Crostini Debian Stretch/perl 5.(12|24). I came up with some shell scripts for testing, and cobbled a Perl sample together with Test::More and Capture::Tiny, but could not seem to come up with a Test:: module or other TAP solution that was a good fit for running UNIX script against an input file and comparing the result to a static file with known correct output. I would be interested recomendations if anyone knows something that fits the description.


Ron

Replies are listed 'Best First'.
Re^6: Print the line with the largest number from standard input
by haukex (Archbishop) on Aug 05, 2019 at 09:33 UTC
    Anyone have a good argument for preferring -Mstrict -Mv5.10 against -Mv5.12 ??

    Personally I'd use -wMstrict or -wM5.012. v5.12 was released more than 9 years ago (vs. more than 11 years for v5.10), so I think it's ok to require it. (Then again, if I'm only requiring v5.10 for the // operator, I might just leave the version requirement off entirely.)

    Test::More and Capture::Tiny

    That's more or less how I've done it in the past, sometimes using IPC::Run3 and more recently my own module IPC::Run3::Shell. Here is one example, along with most of the other test scripts in that directory.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103902]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-03-28 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found