Valid input compared with input

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.

You have the @selections array. And you have the @valid_options array. What you want to do is - loop through @selections array, and use grep to check if the current element is present in @valid_options array. If it is, you have a valid option; otherwise not.

$
$
$ perl -le '@valid_options = qw(valid1 valid2); @selections = qw(valid1 valid2 valid3 valid4);
            for $i (@selections) {print "$i is ", grep(/$i/,@valid_options) ? "Valid!" : "Invalid!"}'
valid1 is Valid!
valid2 is Valid!
valid3 is Invalid!
valid4 is Invalid!
$
$

tyler_durden

1 Like

Perfect! Thank you!

sub init {
        use Getopt::Long;
        use Data::Dumper;
        my @selections = ();
        my @supported_apps = ( "mysql", "tomcat" );
        GetOptions( "install=s" => \@selections );
        
        for my $i( @selections ) {
                my $supported = grep( /$i/, @supported_apps );
                if( $supported eq 0 ) {
                        _print( "$i is invalid.  Please input a supported application", 1 );
                        _showHelp();
                        exit;   
                }
        }
}

I know the variable names changed but the idea stays the same. I am writing a small perl installer script so I need to keep an organized list of applications I support for installation.

Thanks again!