Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-18 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found