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

Slices - Perl

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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

A slice is a range of elements in a list. If it is only one element in a list, then it is known as a scalar slice and you refer to it with the $ symbol. If the slice has more than one element you use the @ symbol, because technically, a multi-element slice is itself a list.

Let's take another look at our list of gladiator names. Of those names, I would like to specify that one is the champion. I am going to create a new variable named $champion and assign it the value of the champion's name. Here it is in code:


#!/usr/bin/perl

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

$champion=$gladiators[2];

print $champion;

Here we have stored the name of our champion in the variable. When we print it out we get the following result:

  CountFistula

Now what if we had some tag team champions and wanted to add them to a new list? Remember that there are two values now, and it is no longer a scalar, so you switch back to the @ symbol:


#!/usr/bin/perl

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

@tagchamps=@gladiators[2,3];

print @tagchamps;

This will print out:

  CountFistula TheNutcracker

Just for kicks, go ahead and try this code (I replaced the @ symbols with $ symbols):


#!/usr/bin/perl

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

$tagchamps=$gladiators[2,3];

print $tagchamps;

This will print out:

   The NutCracker

Perl only takes one of the values, as it sees the $ symbol and is only expecting a scalar and not a list.

Printing the Number of Elements in a List

This method is pretty simple, and hardly requires its own section, with a pristine heading and all, but what the heck. If you want to know how many elements are in a list, you can do so in the following manner (just remember to add one to the result, as the result is just the number of the last element in the array, and not really the full length of the array):


#!/usr/bin/perl

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

print $#gladiators;

Here we have the result:

  3

Remember: element indexes begin with 0, so we add one to the number for our true number of elements. Although really, we could just use the following code and not worry about remembering a thing at all:


#!/usr/bin/perl

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

print $#gladiators+1;

Giving us:

  4



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: