in reply to Incrementing a hash of array value

Does this demonstrate what you're after?
#!/bin/perl5 use strict; use warnings; my %hash=(); %hash=( "M01.001" => 0, "M01.001.111" => 0, "M01.001.111.111" => 0, "M01.002" => 0, "M01.002.003" => 0 ); my %HoA; for my $k ( keys %hash ){ $k =~ /(M\d{2}\.\d{3})/; my $parent = $1; push @{$HoA{$parent}}, $k } foreach my $parent ( sort keys %HoA ) { print "$parent\n"; foreach my $i ( 0 .. $#{$HoA{$parent}} ) { my $child = $HoA{$parent}[$i]; next if $parent eq $child; print "\t$child\n"; } print "\n"; }
Produces...
M01.001 M01.001.111 M01.001.111.111 M01.002 M01.002.003
How complex is the hierarchy? Would any 'children' have 'children' too?

The second loop came from perldsc. I think what you need is in there somewhere.

Replies are listed 'Best First'.
Re^2: Incrementing a hash of array value
by Raad (Acolyte) on Jul 26, 2004 at 13:35 UTC
    Thanks for looking into this. This would work for the sample data above but breaks when new nodes are added such as (C01, C01.111, D01...) The children have also their own children and the children have siblings. I am after a way to hide the complexity of viewing all nodes. A perfect example of what I am after is available at: http://www.nlm.nih.gov/mesh/2004/MeSHtree.A.html. It's done through CGI.
      change:
      $k =~ /(M\d{2}\.\d{3})/;
      to:
      $k =~ /(\w\d{2}\.\d{3})/;

      If I still haven't understood you properly I would need to see a more complete example of the real data you are using.