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))
Lua examples - coroutines, error handling, objects, etc

I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here.

Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a table, and you include in the table references to the functions that are to be called when you perform certain actions on that table. See here for a first example (which never the less shares some functionality between different types of data - polymorphism) and here for an example which also overrides various default methods through a metatable. The data these two sample programs use may be found here.

Coroutines provide the Lua programmer with the ability to have several functions active at the same time - akin to parallel processing or threading, if you like to think of it that way. A comparison is sometimes made to Python's generators, but with Lua coroutines you can easily have several active at the same time. This is a great way of storing "state" within a function - it saves messy parameter passing and globals, and it also saves really long blocks of code too. If you want an iterator - anything that you might say "give me the next ..." to, then it's probably best to use a coroutine. There's a new example showing coroutine basics here, and an example in which we have used several co-routines to process different steps in a process here.

If you open a file in Lua, you'll get a file handle back ... or nil if the file fails to open. But WHY did it fail to open? io.open actually returns a second value if the first one is nil - an error string. There's an example here.

During our courses, we set a number of practical exercises so that delegates can try things out, calling on the tutor to talk about the best way to design and implement a task if they wish, with regards to code readability and maintainability. Such as exercise is set here ... calling for the delegates to write a module to meet what is, in essence, a specification and test harness. My own sample answer, written after the delegates had come up with their own answers, is here

In Lua, functions are truely dynamic - you can load them then overwrite them, and you can even define them within conditionals so that they'll be defined in different ways depending on your data. Powerful but dangerous! There's a sample showing you the mechanism here ... and we discuss the pros and cons of using these techniques on our Lua Programming and Learning to program in Lua courses (the former for people with prior programming experience, the latter for those who have never programmed before).

A log file analysis program that I wrote about 2/3 of the way through the course pulled together all the common coding features you might use in a shortish piece of code - loading data from a file, extracting information from it, sorting and producing a formatted report.

And a final example shows how you can split a line of text using string.find and string.sub ... there are other ways (such as string.gfind) which make for shorter code - there's an example of string.gfind here where it is used for filter email addresses from a flow of data.

Please follow the links on this page to see the various source codes ... please have a look at our Lua courses if you want to learn more from us. Public courses run in Melksham, Wiltshire, England; if you have a group of people, we'll run a private course for you - either at our centre, or at your office almost anywhere in the world.

(written 2009-10-15, updated 2009-10-17)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U114 - Lua - Threading and Coroutines.
  [1691] Co-routines in Lua - co-operative processing - (2008-06-29)
  [1699] If you are learning Lua, here are some more examples - (2008-07-06)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [2314] Passing parameters to a coroutine in Lua - (2009-08-01)
  [3395] Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17)
  [4270] Embedding Lua to perform tailored code at an interval - (2014-05-03)

U111 - Lua - Trapping and handling errors.
  [2360] Error Handling in Lua with assert and pcall - (2009-08-13)
  [2706] Error trapping in Lua - no exceptions. - (2010-04-04)

U109 - Lua - Modules
  [2358] Encapsulating shared code in a Lua module - (2009-08-13)
  [4571] Lua - using modules to add your own utilities - (2015-11-04)

U107 - Object Orientation - the Lua way
  [1692] Towards Object Oriented Programming in Lua - (2008-06-30)
  [1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
  [1819] Calling base class constructors - (2008-10-03)
  [2318] For Lua Programmers AND for Town Planners - (2009-08-02)
  [2359] A fresh example - objects the Lua way - (2009-08-13)
  [2701] Is Lua an Object Oriented language? - (2010-04-01)
  [2703] Lua Metatables - (2010-04-02)
  [2710] __index and __newindex in Lua - metatable methods - (2010-04-05)
  [3142] Private and Public - and things between - (2011-01-22)
  [3396] Tables as Objects in Lua - a gentle introduction to data driven programming - (2011-08-17)
  [3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
  [3683] Weak references in Lua - what are they, and why use them? - (2012-04-04)
  [3694] Special __ methods you can use in Lua metatables - (2012-04-12)
  [3727] Using Lua tables as objects - (2012-05-11)
  [3730] What is a metatable? How do I set one up? How do I use them? Lua - (2012-05-12)
  [4117] Is Lua an Object Oriented language? - (2013-06-15)
  [4248] Metatables, Metamethods, classes and objects in Lua - (2014-03-18)
  [4273] Dot or Colon separator between table name and member in Lua - what is the difference? - (2014-05-06)
  [4572] Tables with values and code in Lua - looks like an object? - (2015-11-05)
  [4573] Classic style OO code - in Lua - (2015-11-05)
  [4753] Lua, Tcl, Python, C and C++ courses - at our Melksham HQ or on your site - forward from July 2017 - (2017-07-02)

U106 - Lua - Functions.
  [1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)
  [1697] Sorting in lua - specifying your own sort routine - (2008-07-05)
  [1744] Lua examples, Lua Courses - (2008-08-08)
  [1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
  [2312] What are closures in Lua? - (2009-07-31)
  [2347] Dot, dot, dot in Lua - variable length parameter lists - (2009-08-11)
  [2356] Passing multiple results back from a function - Lua, Python, etc - (2009-08-13)
  [2723] Returning multiple values from a function - Lua - (2010-04-15)
  [2949] Lua - variable function arguments and select - (2010-09-08)
  [3695] Functions are first class variables in Lua and Python - (2012-04-13)

U103 - Lua - Conditionals and loops
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
  [2454] For loops in Lua - (2009-10-14)
  [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)

Back to
For loops in Lua
Previous and next
Horse's mouth home
Forward to
Bright Eyes
Some other Articles
New Web Site for Melksham Chamber of Commerce
Cant connect to local MySQL server through socket /tmp/mysql.sock
Railway Arithmetic
Bright Eyes
Lua examples - coroutines, error handling, objects, etc
Dark to dark
Making executable binaries in Python (or Perl)
Gypsy says
Family Gathering at 404, The Spa
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/2455_Lua ... s-etc.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb