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