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

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

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

    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 <tore@aursand.no>

    "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 Guest

  2. Similar Questions and Discussions

    1. hash of hashes
      Hi all. How can I create $Subject for future population? -- Yours truly, WBR, Paul Argentoff. Jabber: paul@jabber.rtelekom.ru RIPE: PA1291-RIPE
    2. Sorting Hash of Hashes with HEAP module
      Hi, I would like to use the Heap module from CPAN to sort Hash of Hashes by values, keeping track of what pairs of keys belong to what values. ...
    3. XML parse - hash of hashes
      Greetings, I am fairly new to Perl and to XML and I’m trying to (1) parse a XML document (snippet attached), (2) update specified data using a...
    4. Accesing hash of hashes
      Hello: I am trying to create and access a multidimensional hash. For example the following works ====== $route {$routeDest} = $cost ; print...
    5. iterating through hash of hashes
      I'd like to iterate through a hash of hashes and get all the values. How do I do that? The following works for me. But it seems a bit convoluted...
  3. #2

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

    In article <pan.2003.09.16.08.59.04.103199@aursand.no>, 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 Guest

  4. #3

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

    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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval Guest

  5. #4

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

    Tore Aursand <tore@aursand.no> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139