From: "Connie Chan" <[EMAIL PROTECTED]>

> > Are you sure it's a HashOfArrays and not an ArrayOfHashes?
> >
> > #HAO
> > 
> > $data{$key} = ['Jenda', 'Krynicky', '[EMAIL PROTECTED]', ...]
> > 
> > # AOH
> > $data[$i] = {fname => 'Jenda', lname => 'Krynicky', email => 
> > '[EMAIL PROTECTED]', ...}
> > 
> 
> What I have now is something like this :
> @ID = qw (Foo Bar Blaz Bob);
> @Country = qw (UK US HK HK);
> @Gender = qw (F M M F);
> 
> %table{ID} = (ID => \@ID, Country => \@Country, Gender => \@Gender);
> So I got $table{ID}[1] as 'Bar", and I think this is HOA.
> 
> Note : The above array is always same in dimension, and
> the reading way is Foo's location is in UK and gender is F.
> This is fixed, so I can't modify anything on it.

Yes you are right, this is HoA. Whoever designed this datastructure 
should be hunted down and shot though (exagerating a little).
 
> But what I going to do is like this :
> my %newTable = SortTable ( TABLE => \%table, Seq => "Gender=A,
> Country=D, ID=A"); my %newTable = SortTable ( TABLE => \%table, Seq =>
> "ID=A, Gender=A, Country=A"); # where =A =D is in terms of acending
> and decending order.
> 
> That would really complex if have to write one, so I am looking if
> there is an existed module for this.

No I do not think there is anything done that you could use as such.

Here is a function that sorts the structure using one key. I'll leave 
the rest to you:

sub SortHoAbyFiels {
        my ($HoAref, $key) = @_;
        my @indices = (0 .. $#{$HoAref->{$key}});
                # create an array containing numbers from 0 to
                # the maximum index of the arrays in %HoA
        @indices = sort {$HoAref->{$key}->[$a] cmp $HoAref->{$key}->[$b]}
                @indices;
                # order it by the elements of $HoA{$key}
                # instead of sorting the @{$HoA{$key}} array directly
                # we only sort indices into that array
                # we have to sort the other arrays in %HoA as well

        return {
                ID => [@{$HoAref->{ID}}{@indices}],
                Country => [@{$HoAref->{Country}}{@indices}],
                Gender => [@{$HoAref->{Gender}}{@indices}],
        }
        # return a new HoA containing arrays in the order 
        # specified by @indices
}

$newHoAref = SortHoAbyFiels \%HoA, 'Country';

Try to step through the code and see what's happening there.

There is also an artice "Far More than you ever wanted to know about 
sorting" somewhere on the net that should give you more info.

> > Keep in mind that hash is not ordered!
> 
> As illustrated, the order is always dynamic by different Seq, 
> so that's not important here.
> 
> > If you want to sort the AOH using a field you can do it like this:
> > 
> >         @sorted_data = sort {$a->{$key} cmp $b->{$key}} @data;
> >         #ascending
> 
> but how about HOA ?
> 
> > where $key is the name of the field you want to use.
> > If you want to sort using several fields you can do it like this:
> > 
> >         @sorted_data 
> >                 = sort {$a->{$key1} cmp $b->{$key1} or $a->{$key2}
> >                 cmp $b->{$key2}}
> >                         @data;
> 
> Poor me :-<
> I don't really understand the meaning of  "or $a->{$key2} com
> $b->{$key2}}"  here... Would any hints given here ?

The cmp operator returns -1 if the first argument is smaller than the 
second, 1 if its greater and 0 if its the same. So the

        $a->{$key1} cmp $b->{$key1} or $a->{$key2} cmp $b->{$key2}

means

        $a is smaller than $b 
                IF 
                        $a->{$key1} < $b->{$key1}
                or $a->{$key1} = $b->{$key1} and $a->{$key2} < $b->{$key2}
        $a is greater than $b 
                IF
                        $a->{$key1} > $b->{$key1}
                or $a->{$key1} = $b->{$key1} and $a->{$key2} > $b->{$key2}
        $a is the same as $b (in this ordering)
                IF
                        $a->{$key1} = $b->{$key1} and $a->{$key2} = $b->{$key2}

Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to