Ask a Question related to PERL Miscellaneous, Design and Development.
-
Tore Aursand #1
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
-
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 -
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. ... -
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... -
Accesing hash of hashes
Hello: I am trying to create and access a multidimensional hash. For example the following works ====== $route {$routeDest} = $cost ; print... -
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... -
Andreas Kahari #2
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][cut]> $VAR1 = {
> 'element_id' => '8',
> 'elementtype_id' => '3',
> 'children' => [],
> 'parents' => [
> {
> 'position' => '5',
> 'parent_id' => '3'
> },
> {
> 'position' => '3',
> 'parent_id' => '11'
> }
> ]
> };Why do you want to sort it on parent_id? Wouldn't it be the> How should I proceed when I want to sort this hash? I want to sort it by
> 'parent_id', then 'position'.
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
-
Tassilo v. Parseval #3
Re: Sort a hash based on values in the hash stored as arrays of hashes
Also sprach Tore Aursand:
I don't quite see where 'element_id' comes into the game here. Maybe> 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'.
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
-
Anno Siegel #4
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:
I don't follow. If the elements are stored under their element_id, there> 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.
*can* only be one element with id 8 in the hash. How can it exist in two
places?
Since an element can have multiple parents, which parent-id and position> How should I proceed when I want to sort this hash? I want to sort it by
> 'parent_id', then 'position'.
are you going to sort by? Can an element also have no parents?
Anno
Anno Siegel Guest



Reply With Quote

