Perl will flatten your hash into a normal list. Luckily, if you're expecting this, you can capture it in a normal hash again.
A reference is conceptually more complicated, but it solves issues like parameter positioning and the trickiness involved in converting and copying hashes:my %values = ( name => 'chromatic', rank => 'saint', style => 'obnoxious', ); my %rev_values = do_something(%values); foreach my $key (keys %rev_values) { print "$key => $rev_values{$key}\n"; } sub do_something { my %values = @_; foreach my $key (keys %values) { print "$key => $values{$key}\n"; $values{$key} = reverse $values{$key}; } return %values; }
my %values = ( foo => 1, bar => 2, baz => 3, ); my %rev_values = do_something(\%values); sub do_something { my $h_ref = shift; my %backwards; foreach my $key (keys %$h_ref) { print "$key => ", $h_ref->{$key}, "\n"; $backwards{$h_ref->{$key}} = $key; } return \%backwards; }
In reply to Re: How to use pass and return hashes in a subroutine
by chromatic
in thread How to use pass and return hashes in a subroutine
by chariscomp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |