in reply to Is it safe to use join on a hash?
This is why you can assign a hash, equivalently like:
my %hash1 = ('keyA' => 'valA', 'keyB' => 'valB'); my %hash2 = ('keyA', 'valA', 'keyB', 'valB'); my %hash3 = ('keyA' => 'valA'=> 'keyB' => 'valB');
And use it to capture arguments in a subroutine into a hash, like:
sub mySub { my %args = @_ #... do stuff } # and call it equivalently like mySub ('keyA' => 'valA', 'keyB' => 'valB'); mySub ('keyA', 'valA', 'keyB', 'valB');
This is also why you may see the error, "Odd number of elements in anonymous hash". It's because list assignment to a hash must have an even number of elements (i.e., key/value pairs).
Finally, to get back to your question. It is not only safe to use join on the output of keys, but also on the hash itself:
my $stringA = join ',', %hashA; #additionally, this is valid (to further demonstrate the point): my @arrayA = %hashA;
It bears repeating that hash to array assignment, key/value ordering between pairs is necessarily retained, but ordering among tuples is not guaranteed.
update: fixed spelling of comma!
|
---|