Array
This example covers some basics of Perl arrays including:
- Accessing an array item by subscript
- Accessing an element in an array
- Finding an array element
Perl in Action

Perl Code
Just cut-n-paste to reuse.
use strict;
my $i;
my @arr = ("one","two","three","four","five");
my $lngth = @arr;
print "$lngth\n";
# Do not put parenthesis around the variable or the first item in
# array will be returned.
($lngth) = @arr;
print ($lngth,"\n\n");
# Accessing an array item by subscript
# accessing an element in an array, note 0 based
print ($arr[2],"\n");
# Finding an array element
for ($i=0; $i<@arr; $i++)
{
if ($arr[$i] eq "three")
{
print ($arr[$i],"\n");
print ("element ",$i," in the array","\n");
}
}
If you find this site useful and are a book buyer/reader...
Abe Books is one of my affiliates. I only use affiliates that I also purchase products from. I love Abe books. I have purchased many books from them originally cost $40 to $60 for as little as $5 to $10 dollars including shipping costs.
By accessing Abe books and clicking on one of my links I receive a small commission on your purchase that helps pay for this site. You pay no more for your books than if you accessed their site directly. Thanks for your support.
Click on icon to find books on Perl at Abe Books... |
|
|
|
The Following are a list of Perl books I own
- Perl Black Book by Steven Holzner
- Programming Perl by Larry Wall
- Advanced Perl Programming by Sviram Srinivasan
- Algorithms with Perl by Jon Orwant
- Perl DBI by Alligator Descartes
- Mastering Regular Expressions by Jeffrey Friedl
- Perl Cookbook by Tom Christiansen
- Network Programming with Perl by Lincoln Stein
- WIN32 Perl Programming by Dave Roth
- Writing CGI Applications with Perl by Kevin Melltzer
- mod_perl Developers Cookbook by Geoffrey Young
|
 |