Basic Perl Terminology -
Terminology
Variable - If you're new to programming, a variable can be thought of as a container to store values for later use. Perl can store two types of data in a variable: strings and numbers. Values are assigned to variables using the "=" operator.
String - a string can be a single alphanumeric character, or it can be a bunch of them "stringed" together. Strings are denoted in two ways, with single quotes, or double quotes (We will distinguish these later, but for now we will use double quotes). Strings can be assigned to variables, printed, or combined (concatenated) with other strings.
Scalar - There are two types of variable in Perl. The scalar type is one that contain a number or a string of characters. A variable that is designated a scalar has only one component (i.e. it can have only one value in it at a time.) Perl does not care if a scalar's contents is a number or a string, and they are referred to in the same way. When refering a scalar variable, you must preface it with a $. For example, the variable foo can be assigned a number or a string and is referenced like:
$foo=47; #the number 47
$foo="bar"; #the string "bar"
$foo="47"; #the string "47"
Vector (or Array) - A vector is a variable that contains more than one component. These components are indexed, and you can access individual components at random. Think of it as a bunch of scalar variable strung together. If you're new to programming, think of it as a deck of cards. Each card in the deck has a value, and there are 52 total cards in the deck. You can go to the 28th card in the deck and see what the value of the card is there. The entire array is referenced with the @ symbol. When you look up a value in the array, the @ changes to a $, because the value there is a scalar. This is an important thing to remember, as it is a source of many beginners' errors. Vectors always start at 0, not 1. Here's an example
@bar=("This","is","an","array","of","words","that","make","a","sentance");
print $bar[0];
$bar[7]="create";
print @bar;
The first line assigns some scalars in a particular order into the array "bar." The second line will print "This." The third assigns the 7th element of the array "bar" to a new value. This replaces the word "make" with "create." The last line will print the entire array, and the result will be:
Thisisanarrayofwordsthatcreateasentance
Hash Table
This is a special type of vector variable. It has more than one component, but instead of accessing the contents through numeric indexes, you use a word. These special words are called keys. The keys are used to refer to components by name. The hash table symbol is a % sign. You can access and change the components like this:
%foo=("name","Scott","number","47","food","pizza");
print $foo{"name"};
$foo{"food"}="BLT Sandwich";
------------------
Jeffrey Ellison
[email protected]
http://www.eons.com - Free Online Tools for Webmasters