Bijan
Bijan

Reputation: 8602

Perl shorthand for if statement

I have the following code in Perl:

next if ++$count == 1;

which should be the equivalent of

if (++$count == 1) {
    next;
}

Is there a way to run next; if I add more conditions? (i.e. if ++$count is equal to 1 or greater than some other value?

Upvotes: 1

Views: 1687

Answers (2)

Borodin
Borodin

Reputation: 126722

If you want to test the incremented value of $count for two separate conditions then it would be best to increment it in a separate statement rather than testing once against ++$count and once against $count

So your code would be

++$count;
next if $count == 1 or $count > $some_other_value;

Upvotes: 2

cms
cms

Reputation: 5982

I think something like

next if ++$count == 1 or $count > $other_value;

ought to just work. It's quite dense though, and reliant on the side effect of the increment, and the order of precedence

I'd be tempted to add parentheses and move the increment away

$count++;
next if ($count == 1) or ($count > $other_value);

Upvotes: 9

Related Questions