[Columbus-pm] hash of hashes

Smith,Devon smithde at oclc.org
Mon Sep 29 08:04:16 PDT 2008


You need to be more careful about the difference between a hash and a
hash reference. You sorta get them mixed up in your examples.

#Hash:
%list = ( list1 => 'value1',
list2 => 'value2',
list3 => 'value3' );

#Hash Ref:
$list_ref = {
ref1 => 'value1',
ref2 => 'value2',
ref3 => 'value3'
};


#Hash:
$list{list4} = 'value4';

#Hash Ref:
$list_ref->{ref4} = 'value4';

#Hash:
for $key (keys %list ) {
    print "$key\n";
}

#Hash Ref:
for $key (keys %{ $list_ref } ) {
    print "$key\n";
}


-----Original Message-----
From: columbus-pm-bounces+smithde=oclc.org at pm.org
[mailto:columbus-pm-bounces+smithde=oclc.org at pm.org] On Behalf Of Chuck
Day
Sent: Monday, September 29, 2008 10:09 AM
To: Derek B. Smith
Cc: columbus-pm at pm.org
Subject: Re: [Columbus-pm] hash of hashes

This is a forum.  We can start here with the basic anonymous hash:

$list = { key1 => value1,
          key2 => value2,
          key3 => value3 };



This is known as %list.



You can add to this:

$list{key4} = value4;




You can print these:

print $list{key4};




Loop through them:

for $key (keys %{ $list } )
{
         do something;
}




I prefer to put the keys into an array though because you can sort these
better:

@keys = sort(keys %list);
foreach (@keys)
{
         do something;
}



Sorting it differently:

@keys = sort { $list{$b} cmp $list{$a} } (keys %list); @keys = sort {
$list{$b} <=> $list{$a} } (keys %list);





> Chuck,
>
> Wow could you share these notes in electronic fomat? At times I have 
> issues grasping these data structures.
> BTW, will columbus.pm hold a Perl workshop in the spring? I read this 
> on PPW's website. I went last year but cannot go this
year...scheduling conflicts.
>
>
> thank you
>


_______________________________________________
Columbus-pm mailing list
http://columbus.pm.org/
Columbus-pm at pm.org
http://mail.pm.org/mailman/listinfo/columbus-pm




More information about the Columbus-pm mailing list