Search code examples
perlif-statementconditional-statements

Invert a condition in Perl


I want to match on fieldA and everything in fieldB as long as it isn't 456:

if ($fieldA==123 && $fieldB!==456)

What is the syntax I should use for fieldB? Is it !== or !=? I've also seen something like !$fieldB==456. It's really the syntax of fieldB that I'm having issues with.


Solution

  • In general you want to use

    if ($fieldA == 123 && $fieldB != 456)
    

    If you wanted to negate both you could use the form

    unless ($fieldA == 123 && $fieldB == 456)
    

    Which would evaluate to true if both assertions were false.