megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a hash like this:
key=>value 1=>1 2=>1 3=>1 4=>2 5=>3 6=>4 7=>4 8=>8 9=>8
based on this hash, i want to build like levels of directory :
1 - 2 - 4 - 6 - 7 - 3 - 5 8 -9
explanation:
1=>1 therefore 1 is root. Same is with 8.
2 & 3 are sub levels of 1 as shown in diagram. Do i need recursion for this? because number of elements is not fixed.
Any hints how to do this?
Thanks!
Gaurav Talwar

Replies are listed 'Best First'.
Re: how to build directory
by greatshots (Pilgrim) on May 22, 2007 at 09:27 UTC
    I would Form a HashofList (HoL) :-
    my %tree_Hash = ( 1=> [ 1, 2, 3 ], 2=> [ 4 ], 3=> [ 5 ], 4=> [ 6, 7 ], 8=> [ 8, 9 ], );
    Values are maintained as a Key and Keys become pushed as values under each key. (i,e) the level as you want.

      Didn't you mean:

      my %tree_Hash = ( 1=> { 2 => { 4=> { 6 => undef, 7 => undef, }, }, 3 => { 5 => undef, }, } 8=> { 9 => undef, }, );

      This can be build programatically from the existing hash.

      But I'm not sure what exactly the OP wants.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Yes i meant this only :). I have been trying to do this for quite sometime but no success. Can you give me some hints on how to do this?