Perl ... 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") or die;
@x = <fh>;
push(@x,"aaron Perl Ruby Opal\n");
unshift(@x,"bob C Lua Matlab Perl Python\n");
splice(@x,3,0,"chris Lua PHP Perl Java\n");
foreach $emp (@x) {
if ($emp =~ /Python/) {
$name = (split(/\s+/,$emp))[0];
push @pythoners,$name;
}
}
Here's the result of running that:
BirthdayBoy:csr1 grahamellis$ perl skillz
Python with bob hazel leane olivia adam barry harry ken nigel rupert
bob C Lua Matlab Perl Python
antonia Perl XML PHP Tcl/Tk MySQL
barbara Tcl/Tk ASP Ruby Java
chris Lua PHP Perl Java
cherry Perl Java Ruby MySQL
delia XML PHP Java ASP
ethel MySQL Perl Tcl/Tk ASP
florence Ruby PHP Java ASP
BirthdayBoy:csr1 grahamellis$
If you want to take something off a list ...
pop off the end,
shift off the beginning, and
splice - again - to take items out of the middle.
(written 2008-07-09, updated 2008-07-08)
Associated topics are indexed under
P208 - Perl - Lists [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)
[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)
Some other Articles
Configuring Apache httpdThe Story of Well House ManorEnvironment variables in Perl / use EnvFinding operating system settings in PerlPerl ... adding to a list - end, middle, startRunning operating system commands in PerlTraining Course and Business Conference TechnologyFTP server on Fedora LinuxIf you are learning Lua, here are some more examplesDealing with The Press