Home > Archive > PERL Beginners > April 2005 > Sort multi referenced array/hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Sort multi referenced array/hash
|
|
| Michael Gale 2005-04-25, 3:56 am |
| Hello,
I have created a multi-referenced hash /array doing the following:
my $total;
my $machine;
my $serverdata;
start of loop ....
$serverdata->{$machine}->{day} = $total;
$serverdata->{$machine}->{hour} = $total / 24;
....end of loop
I have a loop that gets the data for each machine and stores it.
Now I want to print out the data but I would like to have it sorted by
the value in day.
How would I go about doing this ?
I was doing something like:
foreach my $key (sort %hash{$a} <=> $hash{$b} (keys (%hash))) {
blah blah blah
}
But I don't think I can use the same method because machine is all ways
different ?
Thanks
Michael
| |
| Michael Gale 2005-04-25, 3:56 am |
| Hello,
I did create a new hash containing the machines and the day value,
sorted it and used that sorted hash in my loop to print everything out.
Please let me know if there is a better way.
Michael
Michael Gale wrote:
> Hello,
>
> I have created a multi-referenced hash /array doing the following:
>
> my $total;
> my $machine;
> my $serverdata;
>
> start of loop ....
>
> $serverdata->{$machine}->{day} = $total;
> $serverdata->{$machine}->{hour} = $total / 24;
>
> ...end of loop
>
> I have a loop that gets the data for each machine and stores it.
>
> Now I want to print out the data but I would like to have it sorted by
> the value in day.
>
> How would I go about doing this ?
>
> I was doing something like:
> foreach my $key (sort %hash{$a} <=> $hash{$b} (keys (%hash))) {
> blah blah blah
> }
>
> But I don't think I can use the same method because machine is all ways
> different ?
>
> Thanks
>
> Michael
>
| |
| Lawrence Statton 2005-04-25, 3:56 am |
| > Hello,
>
> I have created a multi-referenced hash /array doing the following:
>
> my $total;
> my $machine;
> my $serverdata;
>
> start of loop ....
>
> $serverdata->{$machine}->{day} = $total;
> $serverdata->{$machine}->{hour} = $total / 24;
>
> ...end of loop
>
> I have a loop that gets the data for each machine and stores it.
>
> Now I want to print out the data but I would like to have it sorted by
> the value in day.
>
> How would I go about doing this ?
>
> I was doing something like:
> foreach my $key (sort %hash{$a} <=> $hash{$b} (keys (%hash))) {
> blah blah blah
> }
>
You have it almost perfect.
Assuming $serverdata looks something like
my $serverdata = { machine1 => { hour => 10 ,
day => 240 },
machine2 => { hour => 7,
day => 7 * 24 } ,
machine3 => { hour => 13 ,
day => 13 * 24 } ,
machine4 => { hour => 9 ,
day => 9 * 24 } } ;
foreach my $key (sort { $serverdata->{$a}{hour} <=> $serverdata->{$b}{hour} } keys %$serverdata) {
... do something with $serverdata->{$key} ...
}
>
> Thanks
>
De nada.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
|
|
|
|
|