@subnets = { { 'id' => '1', 'location_id' => '30', 'subnet' => '255.255.255.0' }, { 'id' => '2', 'location_id' => '13', 'subnet' => '255.255.254.0' }, { 'id' => '3', 'location_id' => '19', 'subnet' => '255.255.0.0' }, }; @filers_info = { { 'id' => '1', 'location_id => '19', 'info' => 'blah1', }, { 'id' => '2', 'location_id => '30', 'info' => 'blah1', }, }; foreach my $subnet (@subnets) { my @found_filers = grep { $_->{'location_id'} == $subnet->{'location_id'} } @filers_info; push @total_filers, @found_filers; } @filer_ids = map{$_->{'id'} } @total_filers;