Electric Type

Multimedia

About Us

News

Help

Intro to Perl for CGI

Page 4 — Scalers, Arrays, and Associative Arrays

All variables are one of three different types: scalar, array, or associative array. What's the difference? A scalar variable is the catch all. It can hold numbers, letters, phrases, whatever. It always has a "$" (dollar sign) before it. So if I wanted to assign a value to the variable $stuff, here's how I'd do it:
A word: $stuff = "rules";A phrase: $stuff = "Colin Rules!";Numbers: $stuff = 2;
You can even add and subtract like this:
$stuff = 2 + 2;$stuff = 4 - 2; 
$stuff would then equal 4 and 2, respectively.

Assignments to variables always happen from left to right. 2 + 2 = $stuff; is not only invalid, it'll get you killed in some neighborhoods. I'm not kidding around.

An array variable is a variable that holds many scalar variables in numbered slots. These slots are added as needed so the variable can grow dynamically. It can shrink, too, but that's just a waste of time. Array variables usually have the @ (at symbol) in front of them. When declaring slots individually, you use a $.

You can declare as many slots as you want right away by doing this:

@stuff = ("1","2","ten","Colin Rules","Perl's for winners");
To get at each slot, you call it by number. So $stuff[3] is equal to "Colin Rules", and $stuff[1] is equal to "2". Wait! Why isn't slot 4 "Colin Rules"? That's easy. Perl, like JavaScript starts indexing at zero. So $stuff[0] equals "1", $stuff[2] equals "ten", and so on. You can find out how many slots the array has by looking at the built-in variable $#stuff. For instance, the value of the @stuff array we defined above would be 4. (Remember, counting starts at 0.)

To declare one slot at a time, do this:

$stuff[0] = "2000000";
Note that $stuff[0], $stuff[30], or $stuff[whatever] have no relation to $stuff from the example above. The only thing they share is the same name, which doesn't conflict because the two variables are of different types. Generally it's good form not to give your variables the same name - which is a perfect segue into Rule #5, recently recovered in a dig in Harlem of all places.

Rule #5 All variables must be named intuitively. Sure, $a may be much quicker to write than $apple, but when you have to go back later to dissect everything, you'll have no idea that $a stands for apple.

Name it for what it does. You can use up to 4 million characters (or something like that) to name your variable, so don't worry about running out of room. And don't run it all together, either. You can't use spaces in variable names, but you can use an underscore to separate the words. For instance, $book_page_i_am_on is a lot easier to understand than $bookpageiamon.

What characters can you use in a variable name? Choose from aAbBcCdD to xXyYzZ. But don't use !, @, #, $, %, ^, &, *, (, ), ~ or any other character that deviates from the alphabet. If you didn't sing it in kindergarten, don't use it now.

All righty! The last type of variable is the associative array. Associative arrays are great for storing related information. They are the easiest of the two arrays (in my mind), because you don't have to remember what slot you stuck something in. Associatives usually start with a % (percentage sign), but like the other arrays, when you call a slot individually, use the $. To declare an associative array, simply do this:

%stuff = ("sport","basketball","bike","trek","name","Don't remember",
"Age","Not old enough to drink");
Every other value in this example is the key to the one after it. So $stuff{'sport'} is "basketball", and $stuff{'name'} is "don't remember", and so on. To declare one slot at a time, do this:
$stuff{'girlfriend'} = "Looking. Any leads? Call me";

OK, now you've got those variables, but what good are they unless you can compare them to something? No good: That's what! To give those variables a purpose, you need Perl operators.

next page»


Dynamic HTML  

Frames  

HTML Basics  

Stylesheets  

Tables  

XML  

Javascript  

Database Connections  

Intro To Perl  

HTML 4.0  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.