|
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 01:05:15)
Associated topics are indexed under P208 - Perl - Lists
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
|
1829 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 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).
|
|