In your first code above, $wo_usr_proj{$proj} is a reference to an array, not an array. You need to dereference this value, as you did in your call to push:
push @{$wo_usr_proj{$project}}, $user;
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:
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"; }
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:
use Data::Dumper; print Dumper \%wo_usr_proj; __END__ $VAR1 = { 'project2' => [ 'WO', 'WO2 ], 'project1' => [ 'WO' ] };
As for your second code snippet, the code is not dereferencing $element, but rather, is creating another reference to $element.

If $scalar is a reference to a scalar value, dereference using $$scalar:
my $foo= "bar"; my $ref= \$foo; print $$ref; __END__ bar
If $arr is a reference to an array, dereference using @$arr:
my @array= qw(foo bar baz); my $ref= \@array; print join ':' => @$ref; __END__ foo:bar:baz
If $href is a reference to a hash, dereference using %href:
my %h= ( foo => 'bar' ); my $ref= \%h; while( my( $k, $v )= each %$ref ) { print "$k -> $v" } __END__ foo -> bar
Please see the pod for perlreftut, perldsc, and perllol for more details.

--sacked

In reply to Re: Re: Re: dealing with arrays as hash values by sacked
in thread dealing with arrays as hash values by goonarific

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.