my ($newtop,$error) = _get_top($wanted_number,$tippy_top_hash); and sub _get_top { my $top_req = shift; my $top_item = shift; if ($top_item->{'ctl_id'} == $top_req) { return ($top_item,undef); } ## Added for speed if (exists($top_item->{$top_req})) { return($top_item->{$top_req},undef); } ## End added for speed foreach my $key (keys %{$top_item}) { next unless ($key =~ /^\d+/); if ($key == $top_req) { return ($top_item->{$key},undef); last; ### probably not needed } else { _get_top($top_req, $top_item->{$key}); } return (undef,"Seems we can't get the top item hash!"); } }