Mr. Shawn H. Corey ha scritto: > On Wed, 2008-12-10 at 21:41 +0530, Anirban Adhikary wrote: > >> DEAR List >> Is it possible what i am looking from the following code >> >> my @merge_err=("a","b","c","d","e"); >> my @dup_err=("1","2","3","4","5"); >> my @load_err=("aa","bb","cc","dd","ee"); >> print "Enter Value:-[load/merge/dup]\t"; >> >> >> ##(for the sake of simplicity I am thinking user will give input between >> these values)########## >> >> chomp(my $option = lc <STDIN>); >> my $err=$option."_"."err"; >> >> >> Now here lies my question Is it possible to convert $err into any of the >> above mentioned array/arrays according to user input? Is it possible? >> >> So that i can print the values of the above array/arrays in a foreach loop? >> >> Thanks & Regards in advance >> Anirban Adhikary. >> > > my %Errors = ( > merge => [ 'a', 'b', 'c', 'd', 'e', ], > dup => [ '1', '2', '3', '4', '5', ], > load => [ 'aa', 'bb', 'cc', 'dd', 'ee', ], > ); > > print "Enter option: "; > chomp( my $option = lc <STDIN> ); > if( exists $Errors{$option} ){ > for my $err ( @{ $Errors{$options} } ){ > print "$err\n"; > } > }else{ > die "invalid option: $option\n"; > } > > > Of course Shawn's solution is better. However, if you really need to accomplish the thing in the terms you stated, you might consider using eval:
my @merge_err=("a","b","c","d","e"); my @dup_err=("1","2","3","4","5"); my @load_err=("aa","bb","cc","dd","ee"); print "Enter option: "; chomp( my $option = lc <STDIN> ); my $err=$option."_"."err"; my @selected_err=eval('@'.$err); die("Bad option: '$option'\n") unless @selected_err; foreach (@selected_err) { print ">$_\n"; ## or do whatever } Again, using a hash is a lot more convenient :) Hope this helps. Cheers paolino -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/