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

Hi,
This is my first question here, so apologizes if is something wrong in my questions ...

Two questions:
1. Is there any hack / cpan module that makes something like next code legitimate in perl ?
Imagine you have a hash of hashes, and you want iterate over it, but you don't want to have to write foreach many times (only one better ;)). I'm adding three new features (foreach my @array, keys_it which return iteratively keys which are writen like "*" in its argument)
my %a; $a{key1}{a}{key2}{b}{key3}{c}{key4}{d}=1; $a{key1}{a}{key2}{b}{key3}{d}{key4}{c}=1; foreach my ($a, $b, $c, $d) (keys_it $a{key1}{*}{key2}{*}{key3}{*}{key4}{*}) { whateveryouwant with your elements of %a; }
I'm boring writting foreach many times ;)
foreach my $key1 (keys %{$a{key1}...}) { my $level1=$a{key1}{$key1}; foreach my $key2 (keys %{$level1->{key2}}) { ... } }
I know, this is a stupid question, but who knows maybe there is something like that, and I didn't found it !
2. Is there an hash shell explorator or even better an ncurses hash explorator?
Imagine again:
%a={a => {b => c}, b => 3}; open FILE, "myhash.txt"; print FILE Dumper (%a); cloes FILE
$ hash_shell myhash.txt
> ls
a
b
> cd a
> ls
b
> cd b
> ls
c
Thanks! Updated Jun 11th 2011: I found Data::DPath which is a concept like my first issue in this post ! Thanks!

Replies are listed 'Best First'.
Re: Two questions about hashes
by JavaFan (Canon) on May 30, 2011 at 21:45 UTC
    No.

    But you maybe able to store your data as:

    $a{key1}{key2}{key3}{key4}{"a","b","c","d"} = 1; $a{key1}{key2}{key3}{key4}{"a","b","d","c"} = 1; while (my $mkey = each %{$a{key1}{key2}{key3}{key4}}) { my ($a, $b, $c, $d) = split $;, $mkey; ... }
      Hi,
      These kind of hash are usually autovivification instances, which make hard to build like your proposal.
      But in my next program, I will see if it is possible to change my code style so I can write less code with your tip
      Thank you very much!

        If you are building the data structures yourself, you should be able to build them to make using them easy. Knowing so little about what you are doing, it is difficult to give helpful advice. How are you building your data structures? Where does the data come from and how is it organized? What do you want to do with your data structures once you have them? If you post a working example program, complete with input data and desired output, there are probably many people here who would help you improve it.

Re: Two questions about hashes
by ig (Vicar) on May 31, 2011 at 06:54 UTC

    You might have a look at modules like Data::Walk or Data::Nested helpful. If you spend a bit of time going through the Data modules, you will find these and perhaps others that would help you avoid the repetition of nested loops.

      Thank you very much
      
      I read about Data::Nested / Data::Walk, it is very interesting. Probably I will test it in my next perl script, but only if I have not to execute it in remote servers which won't have cpan modules installed,
      
      I think it is a great idea.
      
      Maybe I will test Data::Find too ;-)
      
      
Re: Two questions about hashes
by anonymized user 468275 (Curate) on May 31, 2011 at 09:32 UTC
    I'd do something like:
    Traverse( \%hashofsomethings, sub { warn "example $_[0]"; }); sub Traverse { my $node = shift; my $code = shift; local $_; if (ref( $node ) eq 'HASH' ) { Traverse( $_ ) for values %$node; } elsif(ref($node) eq 'ARRAY') { Traverse( $_ ) for @$node; } else{ $code ->( $node ); } }

    One world, one people

      You should create your BATX letters instead of ...
        What does mean BATX letters ? Is it a joke?
        I didn't understand it ...
      I need previous keys to know what is I'm computing, so this is not a good solution, but yes a good way learning recursion algorithms & perl ;)
      Thank you!