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

Printing Specific Elements - 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

 Here is how we print only particular items from a list:

#!/usr/bin/perl

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

print "These gladiators will mess you up: \n\n";

print @gladiators;

print "\n\n";

print "My favorite gladiator is ";

print $gladiators[2];

print "\n\n";

print "My most feared gladiator is ";

print $gladiators[3];

This gives us the result:

  These gladiators will mess you up:

  Nitro Blaze CountFistula TheNutcracker

  My favorite gladiator is CountFistula

  My most feared gladiator is TheNutcracker

Each element in a list has an index number, starting with zero. In this instance, 'Nitro' is at index 0 and 'TheNutcracker' is at index 3. There are four elements in total within the array.

Another way we can print individual elements in an array is like this:


#!/usr/bin/perl

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

print "I made up these two gladiators: ";

print $gladiators[2] . "\t" . $gladiators[3];

Giving us:

  I made up these two gladiators: CountFistula        TheNutcracker

You may have noticed that when I wanted to print single elements from the list I switched back to the $ symbol. This is because individually, the items in the list are scalar variables. All of the elements in a list combined and separated by a comma are known as literals. That's just a little reference material for you; it always helps to know the lingo.

I know this may be a little confusing at the moment, but maybe this next section will clear the scalar/literal thing up a bit.



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