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))
Using Perl 6 to analyse and report on data

Within the last 24 hours I've got my hands on the first "official release" or Perl 6 - and I'm going to share with you the first practical program that I've written in the language.

Scenario - I have a data file listing the 2540 railway stations on the national network in the UK together with annual ticket sales numbers. I'm looking to identify how many of them have passenger numbers below a threshhold (35) people joining trains there each day, and what proportion of passenger journeys on the network as a whole start from such lightly used stations. It's all to do with new technology, and whether you would really want / need to install electronic ticket systems at the likes of Sandplace, Shippea Hill and Swale.

Let's take a look at the code - this should be understandable for readers who are experienced Perl scripters already - and for others I'll explain the details when you attend one of our courses ;-)

Tell the operating system / command line handler to find the Perl6 program and interpret the rest of th file under that:

  #!/usr/bin/env perl6

Read in the file called rstats2015.txt, separate the lines into separate strings, and store those strings into an array called @stations.

  my @stations = slurp("rstats2015.txt").split("\n");

Set up a number of scalar variables for later use, initialising them to zero, and also set up an empty has called %small:

  my $countStations = 0;
  my $countSmall = 0;
  my $countIncomplete = 0;
  my $countStationPassengers = 0;
  my $countSmallPassengers = 0;
  my %small;


Set up a variable (which we may change later, or read in from the user) to sets the passenger numbers joining trains per day below which we'll regard a station as being too small to warrant its own set of electronic equipment:

  my $daily = 35;

Loop through each of the station records we have read, splitting it into a number of fields which are tab separated:

  for @stations -> $station {
    my @fol = $station.split("\t");


Take a look at the last data value in each record, and count the station and sum the number of passengers if there's fewer than the threshold. If you can't even convert the last field to a number, jump to other code to handle that case:

  try {
    if (@fol[*-1] < 365 * 2 * $daily) {
      %small{@fol[6].gist} = @fol[*-1];
      $countSmall++;
      $countSmallPassengers += @fol[*-1];
  }


If there was a valid passenger number (small or large) add than into the grand total

  $countStationPassengers += @fol[*-1];

And handle the special case of the last field not being a number (it's a station that didn't return any passenger numbers for the year - perhaps because the station isn't open yet ....):

  CATCH {
    default {
      $countIncomplete++;
      }
    }
  }


Finally in the loop, increment the overall count of stations.

  $countStations++;
}


Once all the data has been analysed and summed, let's output some results. The elems method counts the number of elements in a collection, and the floor method rounds a number down to the whole number below:

  my $unders = %small.keys.sort.join(", ");
  print "$unders\n";
  print "Number of small stations: ",%small.keys.elems,"\n" ;
  print "1 passenger in ",($countStationPassengers / $countSmallPassengers).floor ,"\n" ;


Full code [here]. Data file [here].

If you want to learn Perl 6, please get in touch. From March 2016 we'll be running "Learning to program in Perl 6" for newcomers to programming, and "Perl 6 programming" for experienced programmers in other languages. If you're already a Perl (5) programmer, we can help you with learning into Perl 6 - that would be a tailored course, based on your background ... there's no "one size fits all" for intermediate / advanced courses.
(written 2016-01-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P600 - Perl 6
  [582] DWIM and AWWO - (2006-01-30)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [4604] Perl - version 6 - official version launched at Christmas - (2016-01-02)
  [4605] Hello World - in Perl 6 - (2016-01-02)
  [4607] Classes and object - first steps in Perl 6 - (2016-01-02)
  [4608] Introspecion in Perl 6 - (2016-01-02)
  [4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
  [4610] Sorting by key or by comparator - Perl 6 - (2016-01-03)
  [4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)
  [4620] Perl 6 - a Practical Extraction and Reporting example! - (2016-01-11)


Back to
Hello World - in Perl 6
Previous and next
or
Horse's mouth home
Forward to
Classes and object - first steps in Perl 6
Some other Articles
Using Perl 6 to analyse and report on data
Happy New Year. Our customers are our ambassadors
Training course and hotel room prices - 2016
Management view - TransWilts and transport at the end of 2015
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.info/mouth/4606_Usi ... -data.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb