my %hash; push @{$hash{$_}}, $_ for @elements; foreach (keys %hash) { # gets passed an array reference, deref if you so desire do_something_with($hash{$_}) if scalar @{$hash{$_}} > 0; }