Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by clueless newbie (Curate)
on Jul 22, 2019 at 14:00 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103146]
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: (8)
As of 2024-04-18 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found