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