#!/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'

In reply to Re: Print the line with the largest number from standard input by clueless newbie
in thread Print the line with the largest number from standard input by SSSufe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.