Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Perl module P208
Lists
Exercises, examples and other material relating to training module P208. This topic is presented on public courses Perl Programming, Perl bootcamp

A list is a collection of scalars. If you know any other programming languages, you might think this sounds like an array. It does it can do everything that an array can do, and much more too.

Related technical and longer articles
Data Monging
Context - List, scalar and double quotes

Articles and tips on this subjectupdated
3870Writing more maintainable Perl - naming fields from your data records
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") ...
2013-01-01
 
3939Lots of ways of doing the same thing in Perl - list iteration
"There's more than one way of doing it" ... the mantra of Perl, which is both a blessing and a curse. It's a blessing because the programmer has a rich array of tools to hand and can always find a solution, making this ons of the most flexible languages around, and making it quick and easy for the ...
2013-01-01
 
3906Taking the lead, not the dog, for a walk.
When I say that "I'm taking the dogs for a walk", I don't literally carry the dogs for a walk - they walk themselves and I just hold the leads. Statement of the obvious? Yes! But you would be surprised how many programmers pass complete full data structures into named blocks of code - "subroutines" ...
2012-11-03
 
3669Stepping through a list (or an array) in reverse order
If you want to iterate through a list in reverse order, you can do so by stepping over (iterating through) the indexes of the list in reverse order. So in Python, for example, you would use range (or xrange for a potentially long list) with an increment of -1, giving the final index number as the start ...
2012-03-24
 
3548Dark mornings, dog update, and Python and Lua courses before Christmas
It's significantly darker each morning as we take Gypsy and Billy out for a morning walk. We're out well before sunrise now, which was at 07:29 on his first morning with us, it is at 08:04 today - just a fortnight later - and will be at 08:16 by 31st December. Only from 1st January does it start getting ...
2011-12-17
 
3400$ is atomic and % and @ are molecular - Perl
"When do I use a $, when do I use an @, and when do I use a % ?" That is a frequently asked question on a Perl course, where a delegate had dabbled with a bit of Perl ahead of time. You use @ if you're referring to the whole of a list - in other words a complete collection. If you're a chemist, you ...
2011-08-20
 
463Splitting the difference
Perl's split function takes a string of text, and divides it up at a delimiter of your choice into a list of shorter strings ... it's one of the "power tool" functions of Perl and a vital part of the language. So how come that you can write a Tcl program and use its version of split - or omit the split ...
2011-03-01
 
2996Copying - duplicating data, or just adding a name? Perl and Python compared
When you copy a list in Perl, you're duplicating the data and you end up with two distinct copies ... but when you copy a list in Python, you're copying the reference so that you end up with two names for the same variable - almost like an alias. So in Perl - with two different copies - you end up with ...
2010-10-30
 
2833Fresh 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 ...
2010-06-27
(longer)
2813Iterating over a Perl list and changing all items
You can loop through a list (we're supposed to call them "lists" not "arrays" in Perl these days!) using a foreach loop running a counter, or using the second form of foreach that doesn't provide a counter. Thus [source link]: #!/usr/bin/perl @demo1 = (20,30,50,60,80,200); @demo2 = (40,70,90,40,20,10,0); ...
2010-06-18
 
2484Finding text and what surrounds it - contextual grep
grep is a very useful tool for finding all the lines in a file (or series of files) that contain a particular string or match some other pattern / criteria. For example, I have a file that contains data for 52 staff members and I want to find who knows Lua ... [trainee@melksham Download]$ grep Lua ...
2009-10-30
 
2295The dog is not in trouble
"I know I put my papers somewhere" I said to Lisa [wife], and Gypsy [dog] goes off and whimpers in the corner, looking very guilty. So had she [dog] taken the papers and chewed them? That wasn't the case - she had heard the word "no" (or rather "know") in what I said, and had taken it that she was ...
2009-07-18
 
2226Revision / Summary of lists - Perl
We've started the third day of this week's Perl course with a revision of some features of lists ... posting here as there are some useful reminders (and I came back later to add more comments!) # Revision / Summary of lists - Perl   $abc = 16; # single value @abc = (16,14,12,11,10,8,7,2); # ...
2009-06-10
 
2067Perl - lists do so much more than arrays
open (FH,"ac_20090225"); @stuff = <FH>; @interesting = grep(/horse/i,@stuff); print @interesting; In Perl, an @ in front of a variable makes it into a list - and a list variable has the advantages of an array in other languages ... without the disadvantages. And it can do a lot more too. In the ...
2009-03-05
 
928C++ and Perl - why did they do it THAT way?
"Why did [they] do it THAT way?". It's a question often asked by the brighter and more perspective delegates on courses concerning some features of a language that I'm teaching them. And the answer "because they did" is a poor one. It's like saying to a child "because I said so" rather than looking ...
2009-01-01
 
1917Out of memory during array extend - Perl
Use a hash if you want a sparse list in Perl! If you set up a list in Perl and don't fill all the members from zero up, that's OK but all the missing elements will actually use up some memory as they'll point to an "undef" value. So this happens: Dorothy:ppcsrd08 grahamellis$ perl $num[1223776336] ...
2008-12-10
 
1918Perl Socket Programming Examples
It's always a pleasure to run private courses - for when a questions that's a little bit away from the normal comes up, I can take time to provide a full answer and demonstration without fear of loosing delegates from other companies who have no interest. And so it was today - I have two examples, plus ...
2008-12-03
 
1828Perl - map to process every member of a list (array)
Perl's map function (like array_walk in PHP) allows you to do something to every member of a list - thus it often saves you the need to write a loop into your Perl, saves the Perl runtime going back time and again to the interbal byte code generated by the compiler, and can be very efficient. There ...
2008-10-10
 
1703Perl ... adding to a list - end, middle, start
You can add an extra element on to a Perl list with push, into the middle with splice and onto the beginning with unshift. You can extract a single element from splitting a scalar and calling up the element you want using square brackets and the appropriate subscript. Example: open (fh,"../../requests.xyz") ...
2008-07-08
 
1316Filtering and altering Perl lists with grep and map
In Perl, you can process whole lists (arrays) in single operations - and that's very efficient at run time as it avoids the needs for loops, and the needed for Perl's Virtual Machine to go back to the byte code (in effect source) for each member. The map function applies an opertion to each member of ...
2007-08-23
 
1304Last elements in a Perl or Python list
How do we refer to the elements of a list? By index number, starting at zero and stopping one short of the number of elements in the list. So a 20 element list has element numbers 0 to 19. How can we refer to the last element, then? We could write an expression based on the length of the list - ...
2007-08-16
 
968Perl - a list or a hash?
You can hold multiple scalars in either a list or a hash in Perl. A list (signaled by an @ character or [..] around the subscript) is ordered - i.e. the elements are numbered and the order is significant. In contrast, a hash (signalled by a % character or {..} around the subscript) is unordered; the ...
2006-12-07
 
773Breaking bread
How can I take ONE thing, discard some of it, and end up with MANY things all of the same type as the original? Surely that doesn't make sense does it? I could take a packet of half a dozen rolls from the supermarket, made (as they often are in the UK) as a single piece of bread with narrow "tear" ...
2006-06-22
 
762Huge data files - what happened earlier?
When I'm programming a log file analysis in Perl, I'll often "slurp" the whole file into a list which I can then traverse efficiently as many times as I need. If I need to look backwards from some interesting event to see what happened in the immediate lead up to it, I can do so simply by looking at ...
2006-06-15
 
560The fencepost problem
If you erect a fence of 10 panels, you'll need 11 fenceposts to hold it up. And if you write a program that joins together 10 elements on a line, you'll only need 9 separators between them - this is known as the "fencepost problem". If you write a simple program loop to output each element from a list ...
2006-06-09
 
622Queues and barrel rolls in Perl
There are some applications where you want to go round and round a list and one way to do so is to "barrel roll" the list by moving the last element onto the start each time you use an element. That would be complicated in some languages, but in Perl you can simply use a pop followed by an unshift. ...
2006-06-09
(short)
355Context in Perl
If you write a Perl program, you refer to lists with an "@" symbol in front of the list variable name. But depending on the context of how you write it, the program may interpret it asThe list contents (if the operation is normal for a list) The length of the list (i.e. the element count) if that's ...
2006-06-05
 
Examples from our training material
404h   Exercise answer - looking for all "404" accesses from certain hosts
404hunter   Look for all "error 404" pages from certain hosts.
back   Setting up, using and looping through a list
booklook   chomp, grep, split, unshift and join example
c1   Context, and splitting a list into named scalars
cont2   List, scalar and double quote contexts
context   context demonstration
ctx   Lists and contexts
dins   Demonstration of some facilities of lists
dmore   dicey list sample answer
dow   Anonymous list example
escape   naming list members
fanlist   List functions such as chomp and push in use
gm   grep to comb, map to alter
grepplus   grep altering the incoming list
im2   Using ~~ to match lists (arrays)
lat.pl   copy a list - duplicates it
li1   First example of a list
li2   looping through a list
linelen   Using a list to count records of various lengths
lx   $ atomic, @ molecular
odd   Shuffling a list
prg   Lottery selection
prog.pl   Iterating through a list
prog2.pl   mapping a list, iterating a list with your own counter
sal2   Context examples
salad   setting up lists, looping through lists, list slices
sameness   File Comparison
towns.xyz   Data file - list of States, Towns, Countries etc
vgrep   Verbose grep - illustration of list slices
yumyum   Elegant Context Demo
Pictures
Chocolate bar fun on a Perl course
Students undertake a Perl practical
With a Perl class in Saudi Arabia
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from http://www.training-notes.co.uk.
Topics covered in this module
Basics.
Creating a list.
Referencing an element in a list.
Changing a list.
The length of a list.
Context.
Summary.
Functions that operate on lists.
Functions that let you manipulate the elements within a list.
Functions that let you re-order a list (returning a new list).
Function to return the length of a list.
Operators that relate to lists.
Operators than manipulate strings and lists.
Iterating through a list.
List slices.
Anonymous lists.
Summary.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses at our training centre in Melksham, Wiltshire, England. It's surprisingly cost effective to come on our public courses - even if you live in a different country or continent to us.

We have a technical library of over 700 books on the subjects on which we teach. These books are available for reference at our training centre. Also available is the Opentalk Forum for discussion of technical questions.


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/resources/P208.html • PAGE BUILT: Wed Mar 6 10:39:20 2013 • BUILD SYSTEM: wizard