Home arrow Perl Programming arrow Page 4 - Beginning Programming Perl(s)

List Data Types - Perl

In the last article, you learned how to install Perl. It wouldn't make a lot of sense to install it on your computer if you never did anything with it, now would it? In this article, you'll take your first steps to becoming part of a wild and crazy breed -- a Perl programmer.

TABLE OF CONTENTS:
  1. Beginning Programming Perl(s)
  2. Storing Data in A Variable
  3. Quoth the Raven
  4. List Data Types
By: James Payne
Rating: starstarstarstarstar / 9
October 10, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Sometimes you store stuff in a folder in real life. Now imagine that you have 26 folders in a drawer alphabetized from A-Z. Since you are a programmer, those files probably contain the names and addresses of girls that have rejected you. If you are a young programmer, you are only using one drawer. If you are older, well...you've switched from drawers to outdoor storage sheds. Either way, that is an example of a List Data Type.

Simply put, a List is a group of scalars put in order. There are a variety of ways to order the scalars, which we will get into later on. If you are familiar with arrays in other programming languages, you'll know that a List is the same thing.

Here is how you store data in a List:


@my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

The above creates a list with five scalars in it. If we do the following code:


#!/usr/local/bin/perl

@my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

print @my_cats;

This would print the following:


  Joni Chachi Richie Fonzie Potsy

I can store numbers in Lists also, or mix numbers and characters if I choose. Perl doesn't care. It's too busy unwrapping that Hershey bar (the one with almonds).

As you can see, Lists must have the @ symbol as the first part of their name. They use the lowercase naming convention (or at least I do) and it's best to separate words with underscores, though in truth you don't have to.

So let's say a girl wants to use my cat program to see the names of all my cats. I'm clearly embarrassed to have so many (or I would be if I really did have that many). So I would create a program that would only show the name of one cat from my List.


#!/usr/local/bin/perl

@my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

print $my_cats[1];

This would print the name Chachi to the screen. I know what you are thinking and you can cool your biscuits right there (within arms reach...I'm hungry). Why didn't it print Joni? Simple. Lists start off at 0 and work their way up. To print Joni and Richie, you would use the following code:


#!/usr/local/bin/perl

@my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '};

print $my_cats[0,2];

You may have noticed in the above examples that I switched the @ symbol to the $ symbol on the last line. In addition to wanting to confuse you, this is also necessary because you are calling individual portions of the item (scalars) and not the entire item itself.

You will note that the sample rules apply for quotes when working with arrays. If I wanted a list of contractions, I would do it this way


#!/usr/local/bin/perl

@my_contractions = {'Won't ', 'Can't ', 'Don't '};

print @my_contractions;

The above code prints:


  Won't Can't Don't

Conclusion

While we are not finished with data types or this series, I am finished with this current tutorial. In the next tutorial, we will wrap up Lists and move on to the third data type, and use operators to manipulate data.

Till then...



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