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
Formatting 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
Perl'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
I 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
Command 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
P211 - Perl - Hashes [240] Conventional restraints removed - (2005-03-09)
[386] What is a callback? - (2005-07-22)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[930] -> , >= and => in Perl - (2006-11-18)
[968] Perl - a list or a hash? - (2006-12-06)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1856] A few of my favourite things - (2008-10-26)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3106] Buckets - (2010-12-26)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
P210 - Perl - Topicalization and Special Variables [493] Running a Perl script within a PHP page - (2005-11-12)
[639] Progress bars and other dynamic reports - (2006-03-09)
[969] Perl - $_ and @_ - (2006-12-07)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1221] Bathtubs and pecking birds - (2007-06-07)
[1232] Bathtub example - (2007-06-14)
[1289] Pure Perl - (2007-08-03)
[1444] Using English can slow you right down! - (2007-11-25)
[1508] How not to write Perl? - (2008-01-15)
[1704] Finding operating system settings in Perl - (2008-07-10)
[1728] A short Perl example - (2008-07-30)
[1829] Dont bother to write a Perl program - (2008-10-10)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
[4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)
[4395] Preparing data through a little bit of Perl - (2015-01-15)
[4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
[4700] Obfurscated code - it might work, but is it maintainable? - (2016-07-02)
P209 - Subroutines in Perl [96] Variable Scope - (2004-10-22)
[308] Call by name v call by value - (2005-05-11)
[357] Where do Perl modules load from - (2005-06-24)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[775] Do not duplicate your code - (2006-06-23)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1921] Romeo and Julie - (2008-12-04)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3833] Learning to use existing classes in Perl - (2012-08-10)
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
P207 - Perl - File Handling [12] How many people in a room? - (2004-08-12)
[114] Relative or absolute milkman - (2004-11-10)
[255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
[616] printf - a flawed but useful function - (2006-02-22)
[618] Perl - its up to YOU to check your file opened - (2006-02-23)
[702] Iterators - expressions tha change each time you call them - (2006-04-27)
[867] Being sure to be positive in Perl - (2006-09-15)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1416] Good, steady, simple example - Perl file handling - (2007-10-30)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
[1709] There is more that one way - Perl - (2008-07-14)
[1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
[1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
[2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
[2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
[2818] File open and read in Perl - modernisation - (2010-06-19)
[2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
[3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
[3830] Traversing a directory in Perl - (2012-08-08)
[3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
Some other Articles
Lorry Parking in MelkshamA course is more than just a chap giving a lectureTeaching examples in Perl - third and final partFresh Perl Teaching Examples - part 2 of 3Are you learning Perl? Some more examples for you!Recording (a macro) in viPoulshot Village FeteDauncey Gardens, MelkshamSharing our programs - easy. Sharing our data - harder.