Sort a hash based on values in the hash stored as arrays of hashes

Discussion in 'Perl Misc' started by Tore Aursand, Sep 16, 2003.

  1. Tore Aursand

    Tore Aursand Guest

    Hmm. I'm not quite sure if I got the subject right, but I'll try to
    explain. :)

    I've got a hash of elements stored like this:

    $VAR1 = {
    'element_id' => '8',
    'elementtype_id' => '3',
    'children' => [],
    'parents' => [
    {
    'position' => '5',
    'parent_id' => '3'
    },
    {
    'position' => '3',
    'parent_id' => '11'
    }
    ]
    };

    Records like the one above is stored in a hash (called %hierarchy), with
    the 'element_id' as key (yeah, I want that value in the hash itself, too).

    The problem: Each element can have more than one parent (as illustrated
    above), a parent with a position assigned to it. The element above will
    exist two places in the hierarchy, as you might figure.

    How should I proceed when I want to sort this hash? I want to sort it by
    'parent_id', then 'position'.

    Thanks in advance!


    --
    Tore Aursand <>

    "You know the world is going crazy when the best rapper is white, the best
    golfer is black, France is accusing US of arrogance and Germany doesn't
    want to go to war."
     
    Tore Aursand, Sep 16, 2003
    #1
    1. Advertising

  2. In article <>, Tore Aursand wrote:
    [cut]
    > $VAR1 = {
    > 'element_id' => '8',
    > 'elementtype_id' => '3',
    > 'children' => [],
    > 'parents' => [
    > {
    > 'position' => '5',
    > 'parent_id' => '3'
    > },
    > {
    > 'position' => '3',
    > 'parent_id' => '11'
    > }
    > ]
    > };

    [cut]
    > How should I proceed when I want to sort this hash? I want to sort it by
    > 'parent_id', then 'position'.


    Why do you want to sort it on parent_id? Wouldn't it be the
    same thing as sorting it on element_id (if the parent_id's are
    the element_id's of the parents)?

    Then, if the id's are unique, sorting on position wouldn't do
    anything, would it?


    Cheers,
    Andreas

    --
    Andreas Kähäri
     
    Andreas Kahari, Sep 16, 2003
    #2
    1. Advertising

  3. Also sprach Tore Aursand:

    > Hmm. I'm not quite sure if I got the subject right, but I'll try to
    > explain. :)
    >
    > I've got a hash of elements stored like this:
    >
    > $VAR1 = {
    > 'element_id' => '8',
    > 'elementtype_id' => '3',
    > 'children' => [],
    > 'parents' => [
    > {
    > 'position' => '5',
    > 'parent_id' => '3'
    > },
    > {
    > 'position' => '3',
    > 'parent_id' => '11'
    > }
    > ]
    > };
    >
    > Records like the one above is stored in a hash (called %hierarchy), with
    > the 'element_id' as key (yeah, I want that value in the hash itself, too).
    >
    > The problem: Each element can have more than one parent (as illustrated
    > above), a parent with a position assigned to it. The element above will
    > exist two places in the hierarchy, as you might figure.
    >
    > How should I proceed when I want to sort this hash? I want to sort it by
    > 'parent_id', then 'position'.


    I don't quite see where 'element_id' comes into the game here. Maybe
    this does what you want (lots of curlies ahead):

    @{ $hierarchy{parents} } = sort {
    $a->{parent_id} <=> $b->{parent_id}
    or
    $a->{position} <=> $b->{position}
    } @{ $hierarchy{parents} }

    The "$a->{position} <=> $b->{position}" is only taken into account when
    the comparison of the parent_id resulted in 0 (which happens when they
    are equal).

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Sep 16, 2003
    #3
  4. Tore Aursand

    Anno Siegel Guest

    Tore Aursand <> wrote in comp.lang.perl.misc:
    > Hmm. I'm not quite sure if I got the subject right, but I'll try to
    > explain. :)
    >
    > I've got a hash of elements stored like this:
    >
    > $VAR1 = {
    > 'element_id' => '8',
    > 'elementtype_id' => '3',
    > 'children' => [],
    > 'parents' => [
    > {
    > 'position' => '5',
    > 'parent_id' => '3'
    > },
    > {
    > 'position' => '3',
    > 'parent_id' => '11'
    > }
    > ]
    > };
    >
    > Records like the one above is stored in a hash (called %hierarchy), with
    > the 'element_id' as key (yeah, I want that value in the hash itself, too).
    >
    > The problem: Each element can have more than one parent (as illustrated
    > above), a parent with a position assigned to it. The element above will
    > exist two places in the hierarchy, as you might figure.


    I don't follow. If the elements are stored under their element_id, there
    *can* only be one element with id 8 in the hash. How can it exist in two
    places?

    > How should I proceed when I want to sort this hash? I want to sort it by
    > 'parent_id', then 'position'.


    Since an element can have multiple parents, which parent-id and position
    are you going to sort by? Can an element also have no parents?

    Anno
     
    Anno Siegel, Sep 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    231
    William James
    Nov 5, 2005
  2. Scott  Gilpin
    Replies:
    2
    Views:
    237
  3. Perl Learner

    Hashes of hashes or just one hash ?

    Perl Learner, Jun 8, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    232
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    235
  5. Replies:
    7
    Views:
    174
    Paul Lalli
    Jun 1, 2006
Loading...

Share This Page