0

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.

3
  • 2
    Why do you think you have to do anything inside the code block? Oct 2, 2014 at 10:13
  • @i alarmed alien: Because {} looks very odd but the longer I look at it the familiar it becomes.
    – giordano
    Oct 2, 2014 at 11:32
  • 2
    You can always put a comment in it if you don't like the lack of content - it will help your future self and other users know why there isn't any code. Oct 2, 2014 at 11:37

3 Answers 3

5

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.

3
  • 1
    How is it different from his second code block? !( $x =~ /foo/) and $x !~ /foo/ are equivalent
    – Zaid
    Oct 2, 2014 at 11:36
  • @GreenAsJade: Thanks, that's it ( if () {} elsif {do something} ... ). I thought about this but I never saw it somewhere so I thought that there must be another way to do it. !( $x =~ /^\d{4}$/ ) is not the same as ( $x !~ /^\d{4}$/ )?
    – giordano
    Oct 2, 2014 at 11:37
  • 1
    Whoa, you can stare at easy conditionals and stil get them wrong! My mistake! Oct 2, 2014 at 12:39
1

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.

4
  • 1
    Thanks for this basic block. I didn't know about this. But I think for this kind of problem I just omit the code after if () {} as you mention. I wonder why you have been downvoted.
    – giordano
    Oct 2, 2014 at 11:27
  • Please comment when down voting.
    – mpapec
    Oct 2, 2014 at 11:35
  • @giordano yes, in your case that is the best option.
    – mpapec
    Oct 2, 2014 at 11:36
  • 2
    In situations like this one might argue that case/switch constructs have a much cleaner look and feel. Having said that, I'd personally just stick with if/elsif and let the conditionals do the rest.
    – Zaid
    Oct 2, 2014 at 11:54
0

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 $_;
      }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.