for my $k1 (keys %hash) { for my $k2 (keys %hash) { next if $k1 eq $k2 and $skip_identical_keys; $cart_prod{$k1}{$k2} = ...; } }