use strict;
my @strings = qw/
string1
string2
another_string1
another_string2
/;
my %h;
foreach my $string (@strings) {
if ($string =~ /(\d+)/) {
push @{$h{$1}}, $string;
} else {
print "cannot classify $string : skipping\n";
}
}
foreach my $key (sort { $a <=> $b } keys %h) {
foreach my $s (@{$h{$key}}) {
print $s . "\n";
}
}
More detailed than ysth solution , but I hope this helps. Essentially: I use a hash %hwhere the keys are numbers (matching the end of lines), and the values ββare arrays containing strings ending with that number. After creating the hash, I will print its contents, sorting the keys (i.e. the numbers at the end of your lines).
source
share