in reply to Parsing through a menu hash.

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/

Replies are listed 'Best First'.
Re^2: Parsing through a menu hash.
by polettix (Vicar) on Sep 06, 2005 at 20:14 UTC
    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.