giordano
giordano

Reputation: 3162

Perl: how to cope with several condition

I would like to change a variable but if a first condition is true jump to the end of the if-block, respectively, do nothing. This pseudo code shows what I want:

if ( $x =~ /^\d{4}$/ ) {  # first condition
    $x = $x;
} 
elsif ( cond2 ) {         # second condition
    do something with $x;
}
elsif ( cond3  ) {        # third condition
    do something with $x;
}

I don't like the code above since I find it strange to assign the variable to itself. An other solution to avoid this self assignment is:

if ( $x !~ /^\d{4}$/ ) {
    if ( cond2 ) {            # second condition
        do something with $x;
    }
    elsif ( cond3  ) {        # third condition
        do something with $x;
    }
}

What I don't like on this code is that it is nested (what makes it complicated). I would like to have something like this:

if ( $x =~ /^\d{4}$/ ) {  # first condition
    stop here and go to the end  of the if block (END)
} 
elsif ( cond2 ) {         # second condition
    do something with $x;
}
elsif ( cond3  ) {        # third condition
    do something with $x;
} (END)

I know that there are commands last and next but as I understand these commands they works for getting out of a loop.

Any idea how to write a simple nice code for this problem?

Thanks for any hint.

Upvotes: 0

Views: 145

Answers (3)

Borodin
Borodin

Reputation: 126772

You may like the idea of using for as a topicaliser, which shortens your regex matches as well as allowing you to use last to skip the whole block.

A lot depends on what cond2, cond3 and do something with $x look like, but something like this could work

for ($x) {
   last if /^\d{4}$/;
   if ( cond2 ) {
       do something with $_;
   }
   elsif ( cond3  ) {
       do something with $_;
   }
}

or retaining the nesting and allowing unless as the regex pattern match is now a lot shorter

for ($x) {
   unless (/^\d{4}$/) {
      if ( cond2 ) {
          do something with $_;
      }
      elsif ( cond3  ) {
          do something with $_;
      }
   }
}

Upvotes: 0

GreenAsJade
GreenAsJade

Reputation: 14685

You are mistaken about how if-else works.

Where you wrote

if ( $x =~ /^\d{4}$/ ) {  # first condition
    stop here and go to the end  of the if block (END)

... that is exactly what does happen.

This (which you wrote):

if ( $x =~ /^\d{4}$/ ) {  # first condition
    $x = $x;
} 
elsif ( cond2 ) {         # second condition
    do something with $x;
}
elsif ( cond3  ) {        # third condition
    do something with $x;
}

is the same as this

if ( $x =~ /^\d{4}$/ ) {  # first condition
    # do nothing
} 
elsif ( cond2 ) {         # second condition
    do something with $x;
}
elsif ( cond3  ) {        # third condition
    do something with $x;
}

When you understand this, what you are trying to to might seem more obvious.

Upvotes: 5

mpapec
mpapec

Reputation: 50677

You can use basic block to exit your code

BLOCK: {

  if ( $x =~ /^\d{4}$/ ) {  # first condition
      # stop here and go to the end  of the if block (END)
      last BLOCK;
  } 
  elsif ( cond2 ) {         # second condition
      do something with $x;
  }
  elsif ( cond3  ) {        # third condition
      do something with $x;
  }
}

but since you're having elsifs you can omit code for first condition which would have same effect.

Upvotes: 1

Related Questions