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 {
[snip, code omitted]
>    }
> }

If this is really what your code looks like, with unused blocks and all,
it's better written as:

    if (   $add_alias && $add_destination && !$selection
        && $add_alias       !~ /[^\w\.\-]/]
        && $add_destination !~ /\@/
    ) {
        # ... code goes here ...
    }

Format to taste, of course.  Your checks on $add_alias, $add_destination,
and $selection should also probably be checks for defined'ness, not truth. 
0 is false, but, according to your definition of what $add_alias should
contain, is also a valid value.


 
> 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.

Regardless of how you decide to do block your code, this is how your new
code should probably be inserted:

     open(MATCH, $filename);
       while (<MATCH>) {
         @existing = split(/\@/, $_);
         if ( $add_alias eq $existing[0] ) {
           print "That alias already exists!";
           $error=1;
         }
       }
     close(MATCH);

       unless ($error) {
         # ... open FILE, print to it, etc. ...
       }

There are other ways of doing this, such as using subroutines, goto, etc.,
but to me this would be the cleanest.


Also, in your current code you don't do any checks on your calls, such as
your open and rename calls.  You should always check open, and usually check
rename.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to