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.

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:


#!/usr/bin/perl

@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

{mospagebreak title=Printing Specific Elements}

 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: nn";

print @gladiators;

print "nn";

print "My favorite gladiator is ";

print $gladiators[2];

print "nn";

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.

{mospagebreak title=Slices}

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

{mospagebreak title=Replacing an Element Using a Slice OR Slice and Dice}

One method for replacing an element with another element is to use a slice, as demonstrated in the following example. Let’s say that Nitro got mad because his mullet got pulled one too many times and he decided to quit. The crew at Gladiators interviewed a bunch of muscles and decided to go with a five foot four, 300 pound guy with a napoleon complex named Glutious-Minimus. Now they would have to add him to their list and remove Nitro, like so:


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘,’TheNutcracker ‘);

$gladiators[0] = "Glutious-Minimus ";

print @gladiators;

This code does what we set out for it to do: remove Nitro and insert Glutious in his position. If we had said $gladiators[1], then it would have replaced Blaze, and so forth.

Here is the result:

  Glutious-Minimus Blaze CountFistula TheNutcracker

But what if Nitro didn’t quit and the Gladiators just wanted to expand, and hired on our wee friend Glutious? We can add him using the same method; just append him to the end of the list, like so:


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘,’TheNutcracker ‘);

$gladiators[4] = "Glutious-Minimus ";

print @gladiators;

Now we have the result:

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus

And finally, if we want to leave space for another gladiator and don’t want to append them to the end, we don’t have to assign Glutious to position four at all. We could put him at five, six, or whatever position we like. Here, we will put him in position five, leaving four open:


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘,’TheNutcracker ‘);

$gladiators[5] = "Glutious-Minimus ";

print @gladiators;

When you print this out, you cannot tell that four is open. But what if we use our element counter from before?


#!/usr/bin/perl

@gladiators=(‘Nitro ‘, ‘Blaze ‘, ‘CountFistula ‘,’TheNutcracker ‘);

$gladiators[5] = "Glutious-Minimus ";

print $#gladiators;

As you can see, this will print out 5, which isn’t really the amount of elements in the array. Remember, when using this method it simply gives you the last numbered element, not the real number of elements in the array.

Well that’s it for this article. In the next one we will cover some more ways to work with lists and likely delve into hashes as well. So drop by often.

Till then…

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort