Currently the sort call is only sorting a list with one value (the address of the array stored in $wo_usr_proj{$proj}). The code below corrects this by dereferencing the hash element to retrieve the array:push @{$wo_usr_proj{$project}}, $user;
Please take a look at Data::Dumper, as it is invaluable in illustrating the structure of the data in your code. You simply pass the Dumper function a reference to the data structure you wish to see expanded:foreach $proj(sort(keys(%wo_usr_proj))) { print RHANDLE "$proj:\n";$wo_usr_proj{$proj} # note dereference: @{ $array_ref_here } foreach $element(sort @{ $wo_usr_proj{$proj} }) { print RHANDLE "$element\n"; } print RHANDLE "\n\n"; }
As for your second code snippet, the code is not dereferencing $element, but rather, is creating another reference to $element.use Data::Dumper; print Dumper \%wo_usr_proj; __END__ $VAR1 = { 'project2' => [ 'WO', 'WO2 ], 'project1' => [ 'WO' ] };
If $arr is a reference to an array, dereference using @$arr:my $foo= "bar"; my $ref= \$foo; print $$ref; __END__ bar
If $href is a reference to a hash, dereference using %href:my @array= qw(foo bar baz); my $ref= \@array; print join ':' => @$ref; __END__ foo:bar:baz
Please see the pod for perlreftut, perldsc, and perllol for more details.my %h= ( foo => 'bar' ); my $ref= \%h; while( my( $k, $v )= each %$ref ) { print "$k -> $v" } __END__ foo -> bar
In reply to Re: Re: Re: dealing with arrays as hash values
by sacked
in thread dealing with arrays as hash values
by goonarific
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |