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

Dear Monks,

I seek your enlightenment again!

How does one easily parse through a hash of hash of hash of arrays? I'd like to treat the first 3 hashes as menus and the last value will be an array of options.

while(<DATA>) { chomp; my ($one, $two, $three, $four) = split /\:/, $_; push @{$DATA->{$one}{$two}{$three}}, $four; } sub choice { my @a; my $x = 0; while (my ($key, $value) = each %$DATA) { print "$x - $key\n"; push @a, $key; $x++; } print "select : "; chomp(my $a = <STDIN>); my @b; my $c = 0; while (my ($key, $value) = each %{$$DATA{$a[$a]}}) { print "$c - $key\n"; push @b, $key; $c++; } print "select : "; chomp(my $b = <STDIN>); my @d; my $e = 0; while (my ($key, $value) = each %{$$DATA{$a[$a]}{$b[$b]}} ) { print "$e - $key\n"; push @d, $key; $e++; } print "select : "; chomp(my $new = <STDIN>); #PRINT SELECT ARRAY my @counter = 0; foreach(@{$$DATA{$a[$a]}{$b[$b]}{$d[$new]}}) { print "$counter - $_\n"; $counter++; } print "select : "; chomp(my $count = <STDIN>); #DO SOMETHING WITH $counter[$count] } __END__ category1:category2:category3:options categorya:categoryb:categoryc:more options categoryd:categorye:categoryf:even more options

Replies are listed 'Best First'.
Re: Parsing through a menu hash.
by holli (Abbot) on Sep 06, 2005 at 08:36 UTC
    Stuff like this just cries for recursion. Have a look at this:
    use strict; use Data::Dumper; my $DATA; while(<DATA>) { chomp; my ($one, $two, $three, $four) = split /\:/, $_; push @{$DATA->{$one}{$two}{$three}}, $four; } print "#", &choice ($DATA), "#"; sub choice { my $structure = shift; my %menu; my $x = 0; if ( ref($structure) eq "HASH" ) { %menu = map { $x++ => $_ } sort keys %{$structure}; } elsif ( ref($structure) eq "ARRAY" ) { %menu = map { $_ => $structure->[$_] } (0..@{$structure}-1); } else { return $structure; } print map { "$_ - $menu{$_}\n" } sort keys %menu; print "select : "; chomp(my $a = <STDIN>); return choice (ref($structure) eq "HASH" ? $structure->{$menu{$a}} + : $structure->[$menu{$a}]); } __DATA__ category1:category2:category3:options categorya:categoryb:categoryc:more options categoryd:categorye:categoryf:even more options
    This has several favours: better maintainability, easier to read (imho), supports n level depth and multiple "menus" since you pass the data structure into the function.


    holli, /regexed monk/
      An easy support for n level depth can be afforded following merlyn's Recursively walk a hash to get to an element:
      sub pointer_to_element { require List::Util; return List::Util::reduce(sub { \($$a->{$b}) }, \shift, @_); }
      which allows you to write (untested!!!):
      while(<DATA>) { chomp; my @chunks = split /\:/; next unless @chunks > 1; # At least one key, please my $value = pop @chunks; push @{ pointer_to_element($DATA, @chunks) }, $value; }

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
Re: Parsing through a menu hash.
by TedPride (Priest) on Sep 06, 2005 at 09:23 UTC
    You could do something like this, though your output will of course need to be somewhat different:
    use strict; use warnings; my ($d, $k1, $k2, $k3); while (<DATA>) { chomp; @_ = split /\:/; push @{$d->{$_[0]}{$_[1]}{$_[2]}}, $_[3]; } for $k1 (keys %$d) { for $k2 (keys %{$d->{$k1}}) { for $k3 (keys %{$d->{$k1}{$k2}}) { for (@{$d->{$k1}{$k2}{$k3}}) { print "$k1:$k2:$k3:$_\n"; } } } } __DATA__ category1:category2:category3:options categorya:categoryb:categoryc:more options categoryd:categorye:categoryf:even more options
    Or, slightly more complicated:
    use strict; use warnings; my ($d, $k1, $k2, $k3, $p1, $p2, $p3); while (<DATA>) { chomp; @_ = split /\:/; push @{$d->{$_[0]}{$_[1]}{$_[2]}}, $_[3]; } for $k1 (keys %$d) { $p1 = $d->{$k1}; for $k2 (keys %$p1) { $p2 = $p1->{$k2}; for $k3 (keys %$p2) { $p3 = $p2->{$k3}; for (@$p3) { print "$k1:$k2:$k3:$_\n"; } } } } __DATA__ category1:category2:category3:options categorya:categoryb:categoryc:more options categoryd:categorye:categoryf:even more options