Main Content
Saying NOT in Perl, PHP, Python, Lua ... Archive - Originally posted on "The Horse's Mouth" - 2008-07-04 21:04:03 - Graham Ellis
"Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! The ! is the unary not operator in a wide range of languages.
• 2. != Using != means "does not equal". In languages such as C and PHP, it checks the contents of the variables, and in Perl it checks whether two values are numerically the same. In many OO languages, the operator is (or can be) overridden to mean something of your own choice.
• 3. <> This operator in some languages Perl is an alternative to the the != operator.
• 4. ~= In Lua, instead of using != to mean not equals, you used ~=.
• 5. ^ The caret character is used to indicate NOT on of in a regular expression group, and it has the same meaning in Lua's pattern matching too. For example ... [^aeiou] means match any one character which is not a lower case vowel.
• 6. not The word not operates in the same way as the ! operator in many languages. However - it has a different place in the operator precedence tree which means that you'll sometimes need to use round brackets in different places with not and with ! .
• 7. capitalisation In a regular expression in Perl, and in Perl-like regular expression such as PHP's preg functions and Python, you can invert a charactered group b y capitalising the letter. So \s means a space character, but \S means a none-space. Capitalisation also applies in this way in Lua's pattern matching - so for example %a matches an alphabetic character, but %A matches a none-alpha.
• 8. unless In Perl, an unless statement may be used to cause a block of code to be performed only if a condition is NOT true.
• 9. until You'll find until loops in Perl and Lua - a block of code is repeated while a condition is NOT true (i.e. until it is true).
• 10. ne Perl's ne operator checks whether strings are equal. And the -ne operator in shell programming checks whether numbers are equal, using the == operator that Perl uses for numbers for strings instead. (This sort of difference really helps to keep me on my toes when I'm training!)
• 11. !~ "Does not match ..." a regular expression. Perl.
• 12. !== "Is not identical to" in PHP. The difference between !== and != is that the latter operator will return false (indicating two values are equal) if you compare the number zero to either the string 0, or an empty string, or a variable that does not exist. The former will ONLY return false if you compare two values of the same type that have the same value.
Some other articles
Y103 - Conditionals and Loops Conditional operators in Python Equality (in Python) Method, Class, Module, Package - how to they relate in Python? Setting up and tearing down with the Python with keyword Finding sum, minimum, maximum and average in Python (and Ruby) Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) If elif elif elif - multiway selection in Python Identity in Python Flowchart to program - learning to program with Well House Learning to program - the if statement. Python. Finding the total, average, minimum and maximum in a program Python or Lua - which should I use / learn? Python for loops - applying a temporary second name to the same object Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Python - fresh examples from recent courses Groupsave tickets - 3 or 4 train tickets for the price of 2 Learning to program in Python 2 ... and / or in Python 3 This article Equality, sameness and identity - Python Decisions - small ones, or big ones? No switch in Python Python is like a narrowboat Python - when to use the in operator New - Conditional expressions in Python 2.5 Python - block insets help with documentation The ternary operator in Python Wimbledon Neck What - no switch or case statement? U103 - Conditionals and loops repeat until in Lua - a one or more rather than a zero or more loop Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Lazy operators in Lua - what they mean, and examples The goto statement in Lua Python or Lua - which should I use / learn? Does a for loop evaluate its end condition once, or on every iteration? Lua examples - coroutines, error handling, objects, etc For loops in Lua Ternary operators alternatives - Perl and Lua lazy operators Clean code, jump free (Example in Lua) This article Short circuit evaluation (lazy operator) in Lua T203 - Conditionals and Loops Working out distance between places, using OS grid references and a program in Tcl Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Comparing loop commands in Tcl Trapping errors in Tcl - the safety net that catch provides Does a for loop evaluate its end condition once, or on every iteration? Tcl - the danger of square brackets in a while command Tcl - a great engineering language A short form of if ... then ... else Tcl - nice and nasty This article Decisions - small ones, or big ones? Tcl - using [] or {} for conditions in an if (and while) Joining lists in Tcl. Indirect variables in Tcl. R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby Where does Ruby load modules from, and how to load from current directory Separating your code for easier testing, understanding and re-use; example in Ruby Finding sum, minimum, maximum and average in Python (and Ruby) Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Passing code to procedures and yield in Ruby A short form of if ... then ... else Learning to program in Ruby - examples of the programming basics Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) This article Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? Breaking a loop - Ruby and other languages 1st, 2nd, 3rd revisited in Ruby P206 - More Loops and Conditionals While, for, foreach or something else to loop. Ruby v Perl - a comparison example Perl - making best use of the flexibility, but also using good coding standards How a for loop works Java, Perl and other languages Some more advanced Perl examples from a recent course Multiway branches in Perl - the given and when syntax Alternative loops and conditionals in Ruby and Perl Are you learning Perl? Some more examples for you! A pint of Black Rat, and a lazy barman Setting a safety net or fallback value in Perl switch and case, or given and when in Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Question Mark - Colon operator (Perl and PHP) This article Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages The ternary operator in Python What - no switch or case statement? Perl - redo and last without a loop P204 - Conditionals and Loops Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Showing what programming errors look like - web site pitfall Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? Increment operators for counting - Perl, PHP, C and others Are you learning Perl? Some more examples for you! For loop - checked once, or evety time? Ruby v Perl comparison and contrast Do not copy and paste code - there are much better ways Ternary operators alternatives - Perl and Lua lazy operators Equality and looks like tests - Perl This article Learning to program in Perl Decisions - small ones, or big ones? Lexical v Arithemetic testing, Bash and Perl Smart English Output - via PHP and Perl ? : operator -> , >= and => in Perl Wimbledon Neck J704 - Loops and Conditional Statements Java example - for loop and conditionals from course exercise A behaviour driven example of writing a Java program Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Setting up individual variables, and arrays, in Java - some commented examples for and foreach in Java This article Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Wimbledon Neck H104 - Control Statements Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) While, for, foreach or something else to loop. Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Predictions for the seagull population Extracting real data from an exported file in PHP or Perl Question Mark - Colon operator (Perl and PHP) This article Decisions - small ones, or big ones? for loop - how it works (Perl, PHP, Java, C, etc) Testing for one of a list of values. Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages Double and Triple equals operator in PHP The ternary operator in Python Don't repeat code - use loops or functions Assignment, equality and identity in PHP Wimbledon Neck Code and code maintainance efficiency C203 - Conditionals and Loops Learning to program sample program - past its prime, but still useful Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Does a for loop evaluate its end condition once, or on every iteration? Loops - a comparison of goto, while and for Breaking the running sequence - an introduction to conditional statements and loops How a for loop works Java, Perl and other languages New year, new C Course Increment operators for counting - Perl, PHP, C and others Function Prototypes in C New C Examples - pointers, realloc, structs and more This article Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages Wimbledon Neck