Hash table Questions ...
Randal L. Schwartz
merlyn at stonehenge.com
Mon Nov 27 21:07:11 CST 2000
>>>>> "drulea" == drulea <drulea at grassvalleygroup.com> writes:
drulea> Hi,
drulea> Quick question here. I have a hash table with the following fields:
drulea> $log_hash->{time} = $time;
drulea> $log_hash->{email} = $email;
drulea> $log_hash->{name} = $name;
drulea> $log_hash->{update} = $update;
drulea> $log_hash->{tag} = $tag;
drulea> $log_hash->{log} = $log;
drulea> $log_hash->{cr} = $CRnumber;
drulea> I set searchable hash key value to the "$time" field in the following
drulea> manner: "$LOG_HASH { $log_hash->{time} } = $log_hash;"
drulea> The creation and print out of the hash table is OK. For print out, I use the
drulea> following:
drulea> foreach $elem ( keys %LOG_HASH ) # Print out the hash table
drulea> {
drulea> print OUT_HASH ("CR:$LOG_HASH{$elem}{cr} checked in to: ");
drulea> print OUT_HASH ("$LOG_HASH{$elem}{tag} on: ");
drulea> print OUT_HASH ("$LOG_HASH{$elem}{time} by: ");
drulea> print OUT_HASH ("$LOG_HASH{$elem}{name}\n");
drulea> }
drulea> However, I'd like to print out each element of the hash according to its CR
drulea> value, not its time value. For example, I want all the hash values with CR
drulea> values = 1 printed first, next those with CR = 2, etc. Several elements in
drulea> the hash have identical CR values. Any ideas how to do this?
if the possible CR values are known ahead of time:
for my $cr (qw(1 2 3 5 10)) {
for my $elem (sort grep { $cr == $LOG_HASH{$_}{cr} } keys %LOG_HASH) {
# your stuff above
}
}
If you don't know the possible cr values, and want them in numeric order:
my %temp = map { $LOG_HASH{$_}{cr}, 1 } keys %LOG_HASH;
for my $cr ( sort {$a <=> $b} keys %temp ) {
... # as above
}
There. Wasn't so hard, eh?
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
TIMTOWTDI
More information about the Pdx-pm-list
mailing list