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

In reply to Re^5: Print the line with the largest number from standard input by mr_ron
in thread Print the line with the largest number from standard input by SSSufe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.