Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: question about multi value hash key

by kyle (Abbot)
on Mar 31, 2009 at 22:06 UTC ( #754553=note: print w/replies, xml ) Need Help??

in reply to question about multi value hash key

I'm not quite clear on what you're trying to do. It sounds as if maybe you have more than one conference at a time, and you want to store them all under the same hash key.

I suggest that you work with hash references that contain all of the conference information in one hash.

{ time => $time, conference => $value1, type => $value2, comment => $value3, }

You can take one of those and stick it in a hash of arrays (of hashes) or just have an array of them.

push @AoH, { time => $t, conference => $v1, ... };

Then you can sort the array by time when you want to output them.

my @sorted_AoH = sort { $_->{time} <=> $_->{time} } @AoH; foreach my $conf ( @sorted_AoH ) { my $time = $conf->{time}; print ... # element }

If you need random access to these records by time, then you can put them in a hash as you have them but add a layer of array to it.

push @{ $hash{$time} }, { time => $time, type => $v2, ... };

Then you need an extra loop when dealing with them.

for my $time ( sort keys %hash ) { for my $conf ( @{ $hash{ $time } } ) { print ... # $conf element } }

Again, I'm not clear from your description what you're trying to do, but maybe this gives you some ideas.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://754553]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-02-21 08:04 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.