Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Perl Variables: Lists (aka Arrays)

April 26, 1999

So far we've been deluged with information on scalar variables, certainly the most commonly used type of variable in Perl (or any other programming language for that matter). Runner-up in this popularity contest, though, are lists, also commonly known as arrays in other languages. Lists, in brief, allow one variable label to contain a bunch of values, rather than merely one.

We use list variables frequently in real life. A shopping list contains a bunch of values -- names of items. A budget is a list of arithmetic values -- expenditures.

List variables are quite different from scalar variables because they do not share the same types of operations. After all, it doesn't make much sense to compare whether a list of colors is "greater" than a list of pet names (it does make sense to compare whether one list is longer than another, but then the length of a list is a scalar variable).

To prevent variable name confusion, lists in Perl are prefixed with the at-sign character (@). The values contained in a list are called items, and assigning a list of items to a list is quite simple:

@shopping=("milk","eggs","bread","cheetos");

Now then, the list @shopping contains four items, indexed in the order they were created. The first item in the list is "milk", the second is "eggs", and so on. Important: the first item in a list is counted as index number zero. Restated, item 0 in this list is "milk", item 1 is "eggs", and so on.

Here's the confusing bit: when working with lists, we sometimes work with the list as a whole and other times work with particular items from the list. Working with a single item from a list is a scalar activity, as we saw in the earlier sections. You can reference a single item from a list using its index:

$shopping[0]="skim milk";

The above variable refers to the first item in the @shopping list, which has been assigned a new value, "skim milk". Notice, though, that this reference used a $ rather than an @ -- because this one item in the list is scalar. What if you wanted to work with more than one item from the list, but not the whole list? That, then, would constitute a slice, because it is still a list (a list of a list!):

@shopping[0,3]=("skim milk","diet cheetos");

In this example we've assigned new values to two of the items in the list, the first and last items. This time we stuck with the @ prefix, because we have used a slice of the list.

It is quite important to use the correct notation, scalar or list, when constructing these references. Sometimes, using the wrong notion, such as a $ where an @ should be, won't result in an error, per se, but in incorrect results within the program. This is because Perl will interpret the expression one way or another depending on whether you intended to "see" the variable as a list or a scalar at a given moment.

You can determine the length of a list -- that is, how many items it has, using the scalar reference:

$#shopping

The above returns the number of the highest index in the list; in this example, that would be 3. Remember that the list begins at index 0, so a high index of 3 means that this list contains 4 items. If you've ever wondered why programmers are, well, the way they are ... just remember that they have to deal with this kind of thing day in and day out!

Lists provide a convenient means for many types of data sorting, but, again, this tutorial is very much a starter course. Once familiar with the basics of these Perl fundamentals, the twists and turns that come down the road will be that much less dizzying.

Table 2. Perl Comparison Operators
The Perl You Need to Know
Perl Variables: Hashes


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers