Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Print the line with the largest number from standard input (updated)

by haukex (Archbishop)
on Jul 25, 2019 at 17:34 UTC ( [id://11103391]=note: print w/replies, xml ) Need Help??


in reply to Re: Print the line with the largest number from standard input (updated)
in thread Print the line with the largest number from standard input

If the input file contains only a number -9999998, your code will report that, but if that number is -9999999 or below, it reports "No line with number found."

Personally, I use undef instead of a magic number; my usual pattern is the following (expanded slightly for clarity):

my $max; for my $n ( qw/ 6 3 1 4 7 2 10 5 8 9 / ) { if ( !defined $max || $n > $max ) { $max = $n; } } print "Max: ", $max//"none", "\n";

Update: The suggestion to use undef as a special value actually applies to several other posts in this thread as well, at least here and here. OTOH, your code is the only one that uses a well-established module to find the numbers, there are several posts that simply use the original \d+, which of course will cause the code to think that -99 is larger than 98.

Replies are listed 'Best First'.
Re^3: Print the line with the largest number from standard input (updated)
by mr_ron (Chaplain) on Jul 30, 2019 at 14:23 UTC

    I updated my post based on your commentary, thank you for reading it and looking at my code. I think the resulting change was more complicated than the example in your reply, but I felt that was necessary to fit with the surrounding code and keep my solution short.

    Ron
      I think the resulting change was more complicated than the example in your reply, but I felt that was necessary to fit with the surrounding code and keep my solution short.

      Thanks for the update, I think your new code is good, and personally I think it's not too complicated - it's just what is needed for the code to work in all cases :-) If you wanted to shorten it, I'd probably just go with some minor re-wording of the main loop:

      defined( my $l_max = max /$RE{num}{real}/g ) or next LINE; ($f_max,$f_max_line) = ($l_max,$_) if !defined$f_max || $l_max>$f_max;

        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found