my %newhash; foreach my $key ( keys( %hash ) ) { $newhash{$key} = [ map {exists($hash{$_}) ? $hash{$_} : $_;} (values %hash) ]; } %hash = %newhash;