Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by mr_ron (Chaplain)
on Jul 25, 2019 at 14:21 UTC ( [id://11103378]=note: print w/replies, xml ) Need Help??


in reply to Print the line with the largest number from standard input

Some of the other postings give solutions that are more accessible to a beginner, but I would like to present one that demonstrates some possibilities that are available with more advanced knowledge. If the coding looks too complicated to debug and maintain, you might still be able to mine it for some ideas.

Update: based on suggestion in reply from haukex coding now does defined test instead of relying on minimum number. I realize that I have been using the technique of creating variables in a conditional (if ... defined ...) for years without really understanding it, until I came across a posting here and read through related discussion. It may be a bit more advanced than I originally thought but ok fit for the revised solution. Satisfied with the brevity of the result but wish it was less complicated. I learned from working on and revising the code, and hope at least a few readers can learn something useful as well.

#!/usr/bin/perl -n ###################################################################### # Solution requires more advanced knowledge of Perl technology # but leverages "off the shelf" technology and components # for a hopefully short and robust solution. # # -n command line switch provides loop to read file # Number parsing and max number on line come from common # Perl modules. ###################################################################### # # I am being forced to work under threat and duress # and had to remove this. Sorry. Hopefully I can put # it back some day? # # Still afraid of eviction. Harassment at every meal. # Very useful article of clothing seems to have been # stolen day I had to remove code. #

Invocation:

ron@penguin:~/monk-bus/line-w-high-no$ ./high-line.pl <in-data.txt 1 this year is 2019 1

Ron

Replies are listed 'Best First'.
Re^2: Print the line with the largest number from standard input (updated)
by haukex (Archbishop) on Jul 25, 2019 at 17:34 UTC

    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.

      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;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-18 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found