On Thu, Jun 14, 2001 at 01:51:29PM -0500, [EMAIL PROTECTED] wrote:
> i am running through a series of if/elsif checks on a variable:
> 
> if (($add_alias) && ($add_destination) && (!$selection)) {
> 
>    if ( $add_alias =~ /[^\w\.\-]/ ) {
> 
>    } elsif ( $add_destination !~ /\@/ ) {
> 
>    } else {
>       open(FILE, ">>$filename");
>         print FILE "$add_alias\@$domain\t$add_destination\n";
>       close(FILE);
>       open(FILE, "$filename");
>         @entries = <FILE>;
>       close(FILE);
>       @entries = sort(@entries);
>       open(NEWFILE, ">>$tmpfile");
>         foreach $item(@entries) {
>           print NEWFILE "$item";
>         }
>       close(NEWFILE);
>       rename($tmpfile, $filename);
>    }
> }
> 
> this script takes input from a user and adds it into $filename after
> sorting it and checking to verify that $add_alias has only valid a-z A-Z
> 0-9 . - and _ characters and $add_destination has an @ symbol included.
i cant see how your code will check both conditions because of the if - elsif 
construct
> 
> i'd now like to insert a test that will check to see if $add_alias is
> already included in $filename.
> 
>     open(MATCH, $filename);
>       while (<MATCH>) {
>         @existing = split(/\@/, $_);
>         if ( $add_alias eq $existing[0] ) {
>           print "That alias already exists!";
>           $error=1;
>         }
>       }
>     close(MATCH);
> 
> since my if statement is within a while statement, i am not certain how to
> include this within my existing checks and ensure that the script will not
> execute any of the other actions if a match is found.

i would suggest something like this

while (getinput ()) { # pseudo-code to get input

        if (($add_alias) && ($add_destination) && (!$selection)) {

        if ($add_alias =~ /[^\w\.\-]/ ) {
                        handle _error ();       # pseudo-code to handle error
                        next;                           # get input again
                }
        if ($add_destination !~ /\@/ ) {
                        handle_error () 
                        next;
                }
                if (another_check_that_you_were_talking_about) {
                        handle_error ()
                        next;
                }
                # now there must be no errors in the input
                # do something with it
                do_stuff ();
        }
}

here you can put in as many checks as you want. if you use an elsif instead
of an if do_stuff () will be called even if your input passes on check. instead
here it must pass all checks. 

you can also put in the main if ($add_alias && $add_destination && ...)
check along with the others and handle error according to the specific case.

if you dont need any error handling depending on the kind of error you can
put all the checks in one if and join it with an '&&' to ensure all the 
conditions are evaluated.

i hope this is what you are looking for

/kk

Reply via email to