Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Print the line with the largest number from standard input

by clueless newbie (Deacon)
on Jul 22, 2019 at 14:00 UTC ( #11103146=note: print w/replies, xml ) Need Help??


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

#!/usr/bin/env perl use Data::Dumper; # I like to + dump stuff use strict; # "strict" +and "warnings" are a best practice use warnings; my %encountered; # We're goi +ng to "stuff" the numbers encountered here while (<DATA>) { chomp; while (m{(\d+)}g) { # Find numb +ers in the string one set (2019) at a time push @{$encountered{$1}},$_; # Note 1 wi +ll occur twice in the line "1 this year is 2019 1" }; }; # Dump them with a label (and file and line number) so we can verify warn Data::Dumper->Dump([\%encountered],[qw(*encountered)]),' '; # Print them longest first local $"=qq{', '}; #" # To make t +hem more readable for my $number (sort {$b <=> $a} keys %encountered) { # Sort larg +est number first print "$number: '@{$encountered{$number}}'\n"; }; __END__ Hello, i'm 18 1 this year is 2019 1 1 2 3 - 4
yields
%encountered = ( '3' => [ '1 2 3 - 4' ], '2' => [ '1 2 3 - 4' ], '18' => [ 'Hello, i\'m 18' ], '4' => [ '1 2 3 - 4' ], '1' => [ '1 this year is 2019 1', '1 this year is 2019 1', '1 2 3 - 4' ], '2019' => [ '1 this year is 2019 1' ] ); at 11103133.pl line 16, <DATA> line 3. 2019: '1 this year is 2019 1' 18: 'Hello, i'm 18' 4: '1 2 3 - 4' 3: '1 2 3 - 4' 2: '1 2 3 - 4' 1: '1 this year is 2019 1', '1 this year is 2019 1', '1 2 3 - 4'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2019-11-21 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (104 votes). Check out past polls.

    Notices?