Hi everyone,
I cant seem to figure this out for the life of me. Basically, I have two arrays..
sub init {
use Getopt::Long;
use Data::Dumper;
my @selections = ();
my @valid_options = ( "vaild1", "valid2" );
GetOptions( "input=s" => \@selections );
#
# Loop input and compare against valid options storing invalid options
#
}
So as you can see from the above code, I take in input from the user in the command line as --input and want to compare it against valid/supported options.
For example, if they input --input=valid1 --input=valid3 --input=valid2 then it will tell them "valid3 is not a supported option". I have tried embedded loops (for/foreach/etc) and comparing -- as such:
for my $i( @selections ) {
for my $j( @valid_options ) {
if( $i ne $j ) { print "not valid ( $i = $j)"; exit; }
}
}
The above is only an example and I have tried many many many different ways but it seems the caveat is that I need to loop the entire array of inputs first and only erroring if no match at all was found-- this does not satisfy me as I would like to tell the user what they have inputted is wrong and which value exactly is wrong.
In short, I want to validate a users input in comparison with what I dictate as valid options-- the current logic I have tried time and time again does not seem to work as expected because it is simply doing a not equal check which occasionally fails because it is still hitting a valid option BUT just because the two strings do not equal each other, it errors out. This is not what I want.
I hope I have made my problem clear.
EDIT: I understand that I could make this very easy for myself by using static checks but I want to make it all dynamic. For instance, if I want to add another valid input, all I do is append it to the array rather than having to go through the code and add a separate block of code (eg: if( $input == "valid3" ) { ...do this...; } )
For example-- I do not want to have to do this:
foreach my $input( @selections ) {
$input = lc( $input );
if( $input ne "valid1" and $input ne "valid2" ) { print( "Unsupported ($input)" ); }
if( $input eq "valid1" ) { print( "Doing action for valid1" ); }
# etc...
}
Removes the dynamics of it.