On Mon Oct 25 10:18:41 2010, MTHURN wrote:
Show quoted text> It would be nice if there were a way to get the item number (array
> index) of the user's choice, rather than its text value.
Hi Kingpin ;-)
It's not surprising someone would ask for this eventually. There are a
few issues with this request, but I will put it on the to-do list. For
one, if all you want is a simple one-level no frills menu, then use
&pick rather than &Menu. I will implement the third argument as an
index_flag ( to &pick() )that will force the return of the index rather
than the text string (future functionality - not yet implemented).
&Menu is a bit trickier, because options are configured in the hash
block, not the parameter arguments. I will implement this when I get a
chance:
use strict;
use Term::Menus;
my @list=('One','Two','Three');
my %Menu_1=(
Item_1 => {
Text => "]Convey[",
Convey => \@list,
Result => "Index"
},
Select => 'One',
Banner => "\n Choose a /bin Utility :"
);
my @selections=Menu(\%Menu_1);
print "\nSELECTIONS = @selections\n";
### Note the "Index" with the Result key.
### BUT ###
You don't have to wait, you can get what you need with the latest
version of Term::Menus (I am uploading it later today - 1/23/2011. Will
be available via CPAN utility 1/24/2011 after 9:00am EST). Just use this
instead:
use strict;
use Term::Menus;
my @list=('One','Two','Three');
my %Menu_1=(
Item_1 => {
Text => "NUMBER - ]Convey[",
Convey => \@list,
Result => sub {
my $cnt=-1;my $selection=']Selected[';
foreach my $item (@list) {
$cnt++;
chomp($item);
last if -1<index $selection, $item;
} return "$cnt";
}
},
Select => 'One',
Banner => "\n Choose a /bin Utility :"
);
my @selections=Menu(\%Menu_1);
print "\nSELECTIONS = @selections\n";
################
The latest major addition to Term::Menus is the ability to use macros in
anonymous subroutines that can be fed to either the Convey or Result
elements of the Menu hash. With this new capability, the ability to
manipulate data and carry it through an endless variety of Menu
transformations is practically UNLIMITED.