|There's more than one way to do things|
the basic datatypes, threeby root (Scribe)
|on Oct 29, 1999 at 01:53 UTC||Need Help??|
Perl has three basic datatypes: scalars, arrays, and hashes.
ScalarsScalars are the most basic type of data in Perl they can represent a number(int, float, whatever) or a string of text(we won't tell you about scalars as references just yet). Here are some examples of scalar assignments:
One important thing to take note of is that since scalars can be either numbers or strings you need to test for equality in different ways. For numbers use == and != to test for equality and nonequality. For strings you use eq and ne to test for the same things. Here's an example: (Note scalar variables begin with an $)
If you ran this you would see a 1 is placed in the place of $a==$b. When we compare them with a numerical comparison they are processed as numbers and therefore 5.0 and 5 are equal and it returns a 1 which means true in Perl. $a eq $b is replaced with nothing in the print statement because as strings the two variables are not equivalent. This is equivalent to false in Perl along with 0 and a few other things. For information about these type of things check What is true and false in Perl?
For more information on each of these types of scalars check:
Integer Literals in Perl
Float Literals in Perl
Strings in Perl
Functions for SCALARs or strings
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack, q/STRING/, qq/STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
ArraysArrays are basically a collection of scalars stored next to each other and accessed by indices from zero to the number of elements minus one. Here are examples of some arrays in action. Note: when we're referring to an entire array we use the @ at the beginning of its name. If we're referring to only one of its elements(which is a scalar) we use a $.
Arrays can store either strings or numbers or both. Now lets see how we can get at an individual element of an array.
$a; #This returns the first element in @a which is 1 $simpsonsfamily; #This returns the fifth element which is bart $a=4; #This sets the 4th element in @a to 4.
One nice thing about arrays in Perl is that you don't have to set the size of them when you start so they will grow for you when you need them to. To find the size of an array you can you can do scalar(@a) which would return 3 originally and 4 after $a was set to 4; You can get at the highest index by using the variable $# with the arrayname attached to the end. For example $#simpsonsfamily would be equal to 4.
Some other functions I will quickly mention are push and reverse. push is followed by the array you want to add to, and then a value or list of values that you want added to the end. It would look something like:
The reverse function simply takes a list or array and reverses it. For example, to reverse the order of an array permanently you would just type something like:
Functions for real @ARRAYs
pop, push, shift, splice, unshift
HashesHashes are collections of scalars like arrays only they have indices or keys which are strings instead of integers. Hash variables are named with a % followed by at least one letter and then maybe a mix of some more numbers, letters, and underscores. Key and value are two important hash terms. The key is what you use to look up a value. The key is like the index in an array and the value is like the data stored there. Hashes can also be thought of as an array filled with key value pairs as you will soon see. Now we'll show you how you can initialize and access elements of an array.
Now %hash1, %hash2, %hash3 all contain the very same keys and values.
Functions for real %HASHes
delete, each, exists, keys, values
Some other things you might want to check out are:
Control statements and looping
Some things that will make your life easier as a Perl coder