Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
U103 - Lua - Conditionals and loops
  [1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [2454] For loops in Lua - (2009-10-14)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3686] The goto statement in Lua - (2012-04-06)
  [4272] Lazy operators in Lua - what they mean, and examples - (2014-05-05)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4574] repeat until in Lua - a one or more rather than a zero or more loop - (2015-11-05)

P204 - Perl - Conditionals and Loops
  [353] Wimbledon Neck - (2005-06-20)
  [930] -> , >= and => in Perl - (2006-11-18)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1607] Learning to program in Perl - (2008-04-11)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [4031] Showing what programming errors look like - web site pitfall - (2013-03-06)


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
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2351_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb