use warnings; use strict; use Data::Dumper; my $country_hop_ref = {}; while (<>) { chomp; if (/.+?-(([a-z\s+]+?)\s+?([0-9]+?)\s+?hops?)/is) { push @{ $country_hop_ref->{$2} }, $3; } } print Dumper $country_hop_ref;