Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Array Manipulation In Perl
Improve your knowledge of Perl's arrays and hashes with this handy reference guide.

| Back To Basics |

I'll begin right at the top, with the answer to a basic question: what's an array, anyhow?

In all programming languages, an array is a data structure that lets you store multiple values in a single variable. This structure is a very useful method of storing and representing related information. In Perl, array variables look like any other variable, with the exception that array variable names are always preceded by an @ symbol. Here's an example:


#!/usr/bin/perl

# define array
@friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross");


Thus the array variable @friends contains six elements, the names of the "Friends" crew.

The various elements of the array are accessed via an index number, with the first element starting at zero (this is sometimes referred to as "zero-based indexing"). So, to extract the first element of the array above, you'd use the notation $friends[0], while the notation $friends[5] would give you the sixth element of the array, "Ross".  Here's a simple code snippet explaining this:


#!/usr/bin/perl

# define array
@friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross");

# prints "Phoebe"
print $friends[2];


In case you don't want to define the array all at once, you can define it incrementally, by assigning values to the array one at a time using the index notation above. For example, this line of code


#!/usr/bin/perl

# define array
@friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross");


is equivalent to these:


#!/usr/bin/perl

# define array
$friends[0] = "Rachel";
$friends[1] = "Monica";
$friends[2] = "Phoebe";
$friends[3] = "Chandler";
$friends[4] = "Joey";
$friends[5] = "Ross";


Notice that you don't have to do anything special to bring an array variable into existence, like declare it with a keyword or instantiate an object to hold its values - Perl identifies the notation being used and
creates the data structure appropriately.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback