Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Ternary operators alternatives - Perl and Lua lazy operators

"If the gender is male, the answer is him, but if the gender is female, the answer is her". A common situation in programming - [is/are], [him/her], [yes/no], [child/children], [ice/water/steam], [public/private] - and C and Perl and PHP provide the "ternary" operator ? and : to provide a shorthand alternative to a code-heavy if/else structure:

$thing = ($stock == 1) ? "item" : "items";

(That's PHP and Perl to set the "thing" variable to item (singular) or items (plural) depending on the value of $stock being equal (or not) to 1.

In Perl, there's always half a dozen ways to do anything, and the same effect can be created using the lazy and and or keywords ... the logic goes something like this:

i) "If both a and b need to be true and we have already discovered that a is false, we don't need to bother to do test b. And indeed to do test b would be inefficient in such a circumstance. Since (a) is a false value, we'll just return that

ii) "If either a or b needs to be true, and we have already discovered that a is true we don't need to bother to do test b. And indeed to do test b would be inefficient in such a circumstance. Since (a) is a true value, we'll just return that

iii) If we have to move on to test the second item in any circumstance, we may as well just return the value of that item as it's going to be a true value if the whole result is true, and a false value if the whole result is false.

Let's see that in action ... here in Perl:

[trainee@easterton a9_2]$ perl
print (16 and 35)
35[trainee@easterton a9_2]$ perl
print (16 or 35)
16[trainee@easterton a9_2]$


So the result is 16 or 35. By extending that to use both and and or, you'll find that you have a very convenient (and I'm sure planned when the language was written) shorthand for if - else:

[trainee@easterton a9_2]$ perl
print (1 and 4 or 9)
4[trainee@easterton a9_2]$ perl
print (0 and 4 or 9)
9[trainee@easterton a9_2]$


The same thing applies (or rather - ALMOST the same thing) in Lua - where it is especially useful as the language does NOT support the ternary operator:

[trainee@easterton a9_2]$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print (1 and 4 or 9)
4
> print (0 and 4 or 9)
4
> print ( false and 4 or 9)
9
>


I say "almost" the same, because in Lua, the number 0 is a true value, as is an empty string. Only the boolean false, and nil, are really false values. And that's ideal in most circumstances, where you want to be able to accept 0 as just another number - indeed, in Perl 6 you'll be able to say that a number is "0 but true" which rather acknowledges the need for a revision of the "0 is false" mantra.
(written 2009-08-12)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P204 - Perl - Conditionals and Loops
  [4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1607] Learning to program in Perl - (2008-04-11)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [930] -> , >= and => in Perl - (2006-11-18)
  [353] Wimbledon Neck - (2005-06-20)

U103 - Lua - Conditionals and loops
  [4272] Lazy operators in Lua - what they mean, and examples - (2014-05-05)
  [3686] The goto statement in Lua - (2012-04-06)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [2454] For loops in Lua - (2009-10-14)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)


Back to
Ten years in Melksham - looking forward to ten more.
Previous and next
or
Horse's mouth home
Forward to
Printed Directories - the start of the updating season
Some other Articles
See this page in French
Training, Open Source computer languages
All change - Wiltshire Parliamentary
Printed Directories - the start of the updating season
Ternary operators alternatives - Perl and Lua lazy operators
Ten years in Melksham - looking forward to ten more.
Businesses effected by road works in Melksham
Finding literals rather than patterns in Lua
Dot, dot, dot in Lua - variable length parameter lists
The indexed and hashed parts of a Lua table
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2351_Ter ... ators.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat