Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on Jul 22, 2019 at 14:21 UTC ( [id://11103147]=note: print w/replies, xml ) Need Help??


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

Here's some rather excessively commented code in answer to your OPed question. One line that isn't well commented is
    my $largest_n_in_line = (sort { $a <=> $b } @numbers)[-1];
What's going on here? (Update: An alternate statement might be
     my ($largest_n_in_line) = sort { $b <=> $a } @numbers;
In this version, why is the LHS of the assignment enclosed in parentheses? Might it be possible to do this whole operation more simply and perhaps more quickly with an approach based on List::Util::max() instead?)

All the caveats concerning the regex definition of a "number" noted by TieUpYourCamel here also apply. Also, I've chosen not to use chomp although others have. Why might one use or not use chomp in an application like this?

I've also chosen to put the input data from your OP into a separate file and redirect the content of the file to STDIN using the  < command line redirection operator. (I'm using Windows, but this should be exactly the same in *nix IIRC.) I've done this because having to re-type a bunch of test input every time you run your program is going to get very annoying very fast. A separate file is easy to edit, and multiple test files can easily be created. (Update: Of course, you can still enter input manually from the console if you wish.)

Source file line_with_lagest_n_1.pl:

use strict; use warnings; # use Data::Dumper; # for debugging # change this to change what a "number" is. my $rx_number = qr{ \d+ }xms; # these variables are intentionally left undefined. my $largest_n; my $line_with_largest_n; LINE: while (my $line = <STDIN>) { # print Dumper $line; # for debug # extract all number groups from line. my @numbers = $line =~ m{ $rx_number }xmsg; # print Dumper \@numbers; # for debug # nothing to do unless some numbers were extracted. next LINE unless @numbers; # extracted some numbers from this line. find largest. my $largest_n_in_line = (sort { $a <=> $b } @numbers)[-1]; # print Dumper $largest_n_in_line; # for debug # nothing to do unless # NO largest n has been seen so far # OR largest n in line is greater than largest n seen so far. next LINE unless !defined($largest_n) or $largest_n_in_line > $largest_n ; # update largest n and line with largest n. $largest_n = $largest_n_in_line; $line_with_largest_n = $line; # print Dumper $largest_n, $line_with_largest_n; # for debug } # end while LINE if (defined $line_with_largest_n) { print "F$line_with_largest_n"; } else { print "no line with a number was seen \n"; }
Invocation:
c:\@Work\Perl\monks\SSSufe>perl line_with_lagest_n_1.pl < lines.dat F1 this year is 2019 1

Update: I just noticed that the script file name is given above as line_with_lagest_n_1.pl rather than as line_with_largest_n_1.pl as I had intended. But the posted file name is the way it appears on my system, so I'm not going to bother to change it.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-18 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found