use strict; use warnings; my (%hash); while (<>) { chomp; my ($text,$line) = $_ =~ m/(.+)(\d+)$/; $hash{$line} = $text; } print $hash{$_} . "\n" for (sort keys %hash) #### perl pm1.pl < input.txt > output.txt #### use strict; use warnings; my (@array); while (<>) { $array[$2] = 1 if ($_ =~ m/(.+)(\d+)$/) } print defined $_ ? $_ . "\n" : "\n" for @array[1..$#array];