Perl is the
Practical Extraction and Reporting Language, and the data from which you'll want to extract data often comes in the form of CSV (Comma separated variables), or space or tab delimited records.
Opening and reading a file of such records, in a loop, is easy:
open (FH,"trains") or die;
while ($service = <FH>) {
# act in each record here
}
and within the loop, you can split each line into its individuak fields; if the line's tab delimited, for example, you migh write:
@flds = split(/\t/,$service);
That's short and sweet, and I can then refer to individual elements. for example:
print "Train to $flds[2] at $flds[0]\n";
This means, however, that in a complex piece of extraction and analysis code you're likely to be making a large number of references to elements by their position in the original lines, making the bulk of the code harder to follow, and making it difficult to reuse / update the code if the format / field order changes in future data files.
A Better Way
In Perl, you can give a list of scalars on the left of an assignment to name each element of a list (from something like a split) in one go:
($time, $cars, $place, $capa) = split(/\t/,$service);
and you can then refer to the elements by name during your extraction and analysis phase:
print "Train to $place at $time\n";
Although the initial splitting line is (a little) longer, you can now write code that's much more self-documuenting, with meaningful variable names, in the analysis.
And if the field order should even change, you've just got a single splitting line to recode, rather that having to re-engineer the whole analysis phase.
Full example (including sample data and output) is
[here] on our web site. And this example is as taught on this week's
Learning to program in Perl course.
(written 2012-09-25, updated 2013-01-01)
Associated topics are indexed under
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)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[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)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[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)
1ba6
Some other Articles
Using Perl to read an RSS feed off a web site and extract data - via LWP and XML modulesHenbury loop, Bristol - a freight railway line with passenger potential?Trains across Wiltshire - an update on the TransWiltsOn getting noticed for the right reasons when you ask about job availabilityWriting more maintainable Perl - naming fields from your data recordsHow have Melksham shops changed in 60 years?How to make an ohno cakeThe difficult interface between the business, council and voluntary sectorsCelebrate! Buses from Meksham to BathWhat makes Well House Manor different?