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))
Command line and data file use / full course example
Input / Output example from a Well House Consultants training course
More on Input / Output [link]

This example is described in the following article(s):
   • Learning not just what a program does, but how to design it in the first place. - [link]

Source code: log_counter Module: U110

-- Web server log file, "combined" format with vhost in 2nd position

--[[ 192.161.191.101 www.wellho.net - [05/Nov/2015:03:35:03 +0000] "GET
/resources/Y202.html HTTP/1.1" 200 26241 "-" "Mozilla/5.0 (Windows NT 6.1)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" ]]


--[[ Build up example at end of course ...
1. Wrote short main test program
2. Wrote "classes" / tables of functions that it used
3. Got that working
4. Enhanced main program to do other things, adding extra class code
        to be done ... (in other word 'this is how far the example has gone')
5. Refactor to save time and space using metatables, etc
6. Separate out utilisites and "classes" into separate files for late reuse
7. Write it up and test it more
8. Take a long, cold drink (or a warm beer)
]]


Access = {}
function Access.gethost(access) return access[2] end
function Access.getstatus(access) return access[9] end
function Access.factory(raw)
        local hit = {}
        local fn = 1
        local v
        for v in string.gmatch(raw,"%S+") do
                hit[fn] = v
                fn = fn + 1
        end
        hit["getServer"] = Access.gethost -- Should really use metatable technique!
        hit["getStatus"] = Access.getstatus
        return hit
end

-- Main Application -----------------------------------

if not arg[1] then -- Parameter needed
        print("Usage: " .. arg[0] .. " {logFileName}")
        return
end

fh, err = io.open(arg[1],"r") -- Input file not available
if not fh then
        print ("Nah - " .. err)
        return
end

total = 0
counter = {}
while true do
        lyne = fh:read()
        if not lyne then break end
        current = Access.factory(lyne) -- Make me an object
        total = total + 1
        serverName = current:getServer()
        if counter[serverName] then
                counter[serverName] = counter[serverName] + 1
        else
                counter[serverName] = 1
        end
end

print("Total lines of data: " .. total)

-- table.sort(counter) Will NOT work ... sort works on an indexed table.

-- Following code sorts by host name

hosts = {}
for k,v in pairs(counter) do
        hosts[#hosts+1] = k
end
table.sort(hosts)
for k,v in ipairs(hosts) do
        print (string.format("%-30s %6d",v,counter[v]))
end

print (" ----------------------------")
-- resort by number of accesses

function bycount(this, that)
        return counter[this] > counter[that]
end

table.sort(hosts,bycount)
for k,v in ipairs(hosts) do
        print (string.format("%-30s %6d",v,counter[v]))
end

--[[ Sample run

WomanWithCat:ln15 grahamellis$ lua log_counter ac_20151106
Total lines of data: 171578
chs73.info 97
grahamellis.co.uk 61
lisa-ellis.co.uk 4
melksh.am 176
railcustomer.info 26
readtheirobituary.com 22
sheepbingo.co.uk 21
thebutlerdidit.info 8
transwilts.org.uk 74
twhc.org.uk 2876
www.across-the-pond.co.uk 211
www.consultations.org.uk 3
www.firstgreatwestern.info 55277
www.melkshamchamber.org.uk 1194
www.passenger.chat 3
www.savethetrain.org.uk 544
www.twcrp.org.uk 1707
www.wellho.net 108706
www.wellhousemanor.co.uk 568
 ----------------------------
www.wellho.net 108706
www.firstgreatwestern.info 55277
twhc.org.uk 2876
www.twcrp.org.uk 1707
www.melkshamchamber.org.uk 1194
www.wellhousemanor.co.uk 568
www.savethetrain.org.uk 544
www.across-the-pond.co.uk 211
melksh.am 176
chs73.info 97
transwilts.org.uk 74
grahamellis.co.uk 61
railcustomer.info 26
readtheirobituary.com 22
sheepbingo.co.uk 21
thebutlerdidit.info 8
lisa-ellis.co.uk 4
www.consultations.org.uk 3
www.passenger.chat 3
WomanWithCat:ln15 grahamellis$

]]

Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in Lua
 * Lua Programming
 * Lua Programming
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Lua are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Input / Output" training module. You'll find a description of the topic and some other closely related examples on the "Input / Output" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

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/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb