Home arrow Perl Programming arrow Perl: Sailing the List(less) Seas

Perl: Sailing the List(less) Seas

People love lists. Everywhere you look you see them. On magazines, on television. They're everywhere; you can't escape. In this article and the ones that follow, I am going to teach you to blend in with the crowd by using Perl to create lists, multidimensional lists, and hashes, and furthermore, I'll show you how to manipulate each of them.

  1. Perl: Sailing the List(less) Seas
  2. Printing Specific Elements
  3. Slices
  4. Replacing an Element Using a Slice OR Slice and Dice
By: James Payne
Rating: starstarstarstarstar / 2
March 17, 2008

print this article



I know what you are thinking: haven't we talked about this subject before? Yes, yes we did. But only briefly, way back when I went over the various data types in Perl. Here, we will look at them much more closely. So put on your trifocals and get out your bullets, because its time to make us some lists.

Lists: Just a Fancy Way of Saying Array

If you remember the basics of lists, or just can't wait to get to the end of this article, feel free to skip ahead. If you need a refresher in what lists are, then stay tuned.

A list is simply a group of items or elements that are in order. If you know about arrays, then this should seem familiar, as lists are just arrays. If you aren't, then think of a list as a group of variables. Each variable resides within the list and has an index number. The values within the list can be numbers or strings, or numbers and strings. Below is a sample showing how to store data in a list and print out the values:


@gladiators=('Nitro ', 'Blaze ', 'CountFistula ', 'TheNutcracker ');

print "These gladiators will mess you up: ";

print @gladiators;

The above code prints out:

  These gladiators will mess you up: Nitro Blaze CountFistula TheNutcracker

Pretty simple right? You will note a few things about the above code; first, unlike scalar variables, you start the variable name with an @ symbol when dealing with lists, instead of the $ symbol. Each element in the list is encased in a quotation mark, and separated from the other elements in the array by a comma. I also added a space at the end of each element. Had I not, the words would have all printed together.

To print the list, I simply typed print followed by the name of the array. But what if I wanted to print out only certain values in the list? That is covered in the next section

>>> More Perl Programming Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: