use List::Util qw(max); my $max_len_keys = max map length, keys %hash; my $max_len_vals = max map length, values %hash;