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); # multiple values - a list
print "Single value is $abc\n";
print "Single value from \@abc $abc[2] (third element)\n";
print "Whole list \@abc: @abc \n";
# ------ Processing lists as a whole via functions;
# ------ grep to FILTER
# ------ map to transform every element
print "process whole list ...";
# Regulr expression - a 1 follwed by another character NOT a 0 1 or 2
@teens = grep(/^1[^012]/,@abc);
print "Teenagers are @teens\n";
@nextyear = map($_+1,@abc); # Add 1 to each element
push @nextyear,1;
print "Next year ages will be @nextyear\n";
# ------ $#listname tells us top index number
print "You now have ",$#abc," Children\n";
# -------- Handling each element of a list
# ---- use "for" if I need to know the key no.
# ---- use "foreach" if I just want to process content
for ($k=0; $k<=$#nextyear; $k++) { # Start ; end condition; step syntax
print "Child $k will be $nextyear[$k] next year\n";
}
foreach $kiddo (@nextyear) {
print "We will have a $kiddo y.o. child\n";
}
We run
public Perl Programming courses 4 to 6 times a year, and various advanced Perl courses regularly but a little less often - see
here for details.
(written 2009-06-10)
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)
[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)
[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)
[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)
5651
Some other Articles
Running a piece of code is like drinking a pint of beerDo not re-invent the wheel - use a Perl moduleWhere do I start when writing a program?Learning PHP, Ruby, Lua and Python - upcoming coursesRevision / Summary of lists - PerlHow important is a front page ranking on a search engine?Trowbridge - a missed opportunity? Melksham - into the breach?CSS Style Diagrams - working out where attributes come fromA (biased?) comparison of PHP courses in the UKAdding a newsfeed for your users to a multipage PHP application