Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Fresh Perl Teaching Examples - part 2 of 3

Three part article ... this is part 2. Jump to part [1] or [3]


Perl is the most tremendously powerful and fully featured langauge - you've probably seen that from the first set of sample programs from last week's course, which I wrote up over the following weekend to share with a wider audience.

Here ... is part 2 of 3 of those examples.

P207 File Handling and Formatting

on a Well House Consultants Perl CourseFormatting of numbers - especially when it comes to answers which contain monetary amounts - is something delegates are keen to get to quickly. A decision to raise VAT from 17.5 to 20 % leaves us with some interesting decisions on the pricing of hotel rooms - and it also lead me to a demonstration of formatting.

Demonstration source code [here].

I have also posted further thoughts, and sample output [here]





P208 Lists - Perl's Arrays

Delegate on a Perl classPerl's lists do what other language's arrays do and much more - and I often write short demos during courses that show some of the things you can do. Topics such as sorting and context always come up ... when to use an "@" and when to use a "$" are things that many delegates who've tried (and failed) to learn Perl elsewhere come to us needin help with.

Source code of sample early demo is [here]




You can do clever - very clever - things with lists as a whole in Perl (contrast some other languages, where you have to write a loop). Those clever things include filtering a list for items that match a certain pattern (grep) and performing the same operation on every member of an incoming list to give an outgoing list (map).

There's a demonstration that selects all of the towns with "ington" in their names and then SHOUTS them at you ... in one of the new demos.

Source code of demo [here]
Data file that goes with it [here]

A further example using the same data file reformats the lines of data - [source].





P209 Subroutines in Perl

Perl classesI have told the updated story of Romeo and Juliet before - [here] - and how Romeo needed to buy two ladders to get Juliet out for the evening to go down The Tavern, in spite of her dad's objections.

Romeo, being an efficient programmer, used a function to calculate the ladder lengths rather than write the same code twice ... and so this turned in to a "sub" example for my delegates. This is often quite a hard exercise to grasp, as delegates are so concerned towrite code that works that they're not thinking of extensibility.

My sample answer - including a full statement of the question - [here]




Parameters in Perl are passed into subs in the @_ list ... which gives them really horrid names like $_[2] within the sub for the third parameter. So you'll want to take the list of parameters and in most cases copy it into a list of scalars, each of which you'll want to make into a lexically scoped local variable (so it doesn't get mixed up with other variables of the same name). Then you'll want to place your subs in a separate file - that will be a package or namespace that you'll load into all programs that use the same subs with a use statement, or occasionally a require.

I have added three examples - showing each of these steps in turn - onto our site.

Local subroutines using @_ - see [here].
Naming the variables, but keeping the subs in the same file and package - see [here].
And the thirds example - see [here]. It's much shorter, but you'll also need the module that goes with it which is [here]





P210 Topicalization and Special Variables

The fields near MelkshamCommand line parameters may be picked up in Perl in the list @ARGV (you can also read from a file named on the command line through the empty file handle <>.

Sample program - [source]

There's also a sample program [here].




A final example in this section - a "one-liner" which isn't really one line, because the command line options tell Perl to run a loop around the code, and to split each line ... without any actual source at all! See [here]





P211 Hashes

An example written towards the end of a five day Introduction to Programming in Perl course - and it shows the use of lists, hashes, and then loading of a perl module from another file.

Some of the techniques used aren't necessarily best practise as this was a live example, and there are some things shown for clarity rather than efficiency - and clarity for newcomers to Perl code who often find two simpler statements easier than one more complex one. Some variable names were chosen to show flexibility in variable naming, rather than for ease of later maintainance.

Source code - [here]
Module it uses - [here]
Data file - [here]




Using a hash to keep a note of unique words / IP addresses / string values is much more efficient than maintaining a list of all that you've come across so far and having to keep rescanning the list for matches.

Source code example [here]
Data file - [here]




In a hash, keys must be unique, but data value don't need to be so (indeed - the example just above set every value to an identical 1 !)

When you call up a list of the keys of a hash (using the keys function), the keys are returned to you in an order that may APPEAR to be random - but is not; it's all to do with how elements can be accessed quickly and it's called a hashing technique.

If you want to sort a hash, you CANNOT ... but you can sort a list of the keys, and then go through each member of the hash in turn in the order of the sorted keys. It's sort of sorting by the back door ;-). You can even sort the list of keys based on the values they point to in the hash. Clever stuff!

Sample source - one I wrote on the course [here].




To be concluded

Three part article ... this is part 2. Jump to part [1] or [3]


(written 2010-06-27)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P207 - Perl - File Handling
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
  [3830] Traversing a directory in Perl - (2012-08-08)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
  [2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
  [2818] File open and read in Perl - modernisation - (2010-06-19)
  [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
  [2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
  [1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [1442] Reading a file multiple times - file pointers - (2007-11-23)
  [1416] Good, steady, simple example - Perl file handling - (2007-10-30)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [867] Being sure to be positive in Perl - (2006-09-15)
  [702] Iterators - expressions tha change each time you call them - (2006-04-27)
  [618] Perl - its up to YOU to check your file opened - (2006-02-23)
  [616] printf - a flawed but useful function - (2006-02-22)
  [255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
  [114] Relative or absolute milkman - (2004-11-10)
  [12] How many people in a room? - (2004-08-12)

P208 - Perl - Lists
  [3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2813] Iterating over a Perl list and changing all items - (2010-06-15)
  [2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
  [2295] The dog is not in trouble - (2009-07-17)
  [2226] Revision / Summary of lists - Perl - (2009-06-10)
  [2067] Perl - lists do so much more than arrays - (2009-03-05)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [1917] Out of memory during array extend - Perl - (2008-12-02)
  [1828] Perl - map to process every member of a list (array) - (2008-10-09)
  [1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
  [1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [968] Perl - a list or a hash? - (2006-12-06)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [773] Breaking bread - (2006-06-22)
  [762] Huge data files - what happened earlier? - (2006-06-15)
  [622] Queues and barrel rolls in Perl - (2006-02-24)
  [560] The fencepost problem - (2006-01-10)
  [463] Splitting the difference - (2005-10-13)
  [355] Context in Perl - (2005-06-22)
  [240] Conventional restraints removed - (2005-03-09)
  [230] Course sizes - beware of marketing statistics - (2005-02-27)
  [140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
  [28] Perl for breakfast - (2004-08-25)

P209 - Subroutines in Perl
  [3833] Learning to use existing classes in Perl - (2012-08-10)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
  [1921] Romeo and Julie - (2008-12-04)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [969] Perl - $_ and @_ - (2006-12-07)
  [775] Do not duplicate your code - (2006-06-23)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [531] Packages in packages in Perl - (2005-12-16)
  [357] Where do Perl modules load from - (2005-06-24)
  [308] Call by name v call by value - (2005-05-11)
  [96] Variable Scope - (2004-10-22)

P210 - Perl - Topicalization and Special Variables
  [3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
  [1829] Dont bother to write a Perl program - (2008-10-10)
  [1728] A short Perl example - (2008-07-30)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1704] Finding operating system settings in Perl - (2008-07-10)
  [1508] How not to write Perl? - (2008-01-15)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1289] Pure Perl - (2007-08-03)
  [1232] Bathtub example - (2007-06-14)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
  [639] Progress bars and other dynamic reports - (2006-03-09)
  [493] Running a Perl script within a PHP page - (2005-11-12)

P211 - Perl - Hashes
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3106] Buckets - (2010-12-26)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
  [1856] A few of my favourite things - (2008-10-26)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [930] -> , >= and => in Perl - (2006-11-18)
  [738] (Perl) Callbacks - what are they? - (2006-05-30)
  [386] What is a callback? - (2005-07-22)


Back to
Are you learning Perl? Some more examples for you!
Previous and next
or
Horse's mouth home
Forward to
Teaching examples in Perl - third and final part
Some other Articles
Lorry Parking in Melksham
A course is more than just a chap giving a lecture
Teaching examples in Perl - third and final part
Fresh Perl Teaching Examples - part 2 of 3
Are you learning Perl? Some more examples for you!
Recording (a macro) in vi
Poulshot Village Fete
Dauncey Gardens, Melksham
Sharing our programs - easy. Sharing our data - harder.
4252 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/2833_Fre ... -of-3.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat