#!/usr/bin/perl use strict; use warnings;

my @names = qw (Tizio Caio Sempronio);

show_indexed();

my @selected = select_by_index(); print "First choice:\n--->",(join "\n--->",@selected),"\n";

## and then.. push @names,'stevek1974'; show_indexed();

my @selected_bis = select_by_index(); print "Second choice:\n--->",(join "\n--->",@selected_bis),"\n";

################################################################################ sub show_indexed { print "\nChoice one or more name, using their index:\n"; my $index = 0; foreach my $name (@names){ print "[$index]\t$name\n"; $index++; } print "\n"; } ################################################################################ sub select_by_index { my $in = ; chomp $in; # checks are needed in thi case.. unless ($in=~/^[\d,]+$/){warn "WARNING: invalid input!\n";select_by_index()}

my @choice = @names[split /,/, $in];#thanks to tye and choroba: i used @names[eval $in] # more checks are better.. unless (defined $choice[0]){ warn "WARNING: probably you need to check that input numbers are valid for the current array!\n"; select_by_index() } return @choice; }