Perl Information Center Tutorials - Data Types
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Data Types
There are only three types of data in Perl - strings,
integers, and floating point numbers.
strings "dog" "cat" "the_boy"
integers 1 7 -39 37 43 0
floating point 1.2 -17.4 83.2 171.006
All Perl variables contain either one, or a combination of more than one,
of these basic data types. In performing functions which expect a
particular type of data, Perl will automatically adjust the type of data
found in a variable to the type needed by the function.
Variable Types
There are three variable types. Each contains one, or a combination of more
than one, basic data types.
- Scalar - single string, integer, or floating point number
- Array - list of scalars (each scalar can be a different data type)
- Hash - list of name/value scalar pairs
Variables all begin with one of three characters, depending on the variable
type.
- $ - defines a scalar
- @ - defines an array
- % - defines a hash
Scalar
To create a scalar variable, just use it in a line of code
and assign it a string, integer, or floating point value. Any
scalar can be assigned one of the basic data types.
$x = "Hello" # string
$x = 5 # integer
$x = 75.3 # floating point number
One a variable is assigned a data type (value), it can later be
reassigned a new value of another data type.
Array
An array consists of a list of scalar values and can be created
in several ways. Array elements are numbered, with the 0 being the
first array elements. Since array elements are scalars, their variable
name begins with a $.
@x = (3,"dog",1.9) # assign a list of values
$x[5]=3 # assigns value to array element number 6
@newarray = @x[0..3] # slice of values from existing array
$size = @x # $size is # elements in array
$last index = $#x # $lastindex is highest index in array
$#x = 5 # truncates array at 5 elements
@x = () # clears an array
@x = sort @x # sort an array
@x = reverse @x # reverse an array
splice (@x, 0, 5) # remove elements 0-4
@new = splice (@x, 0, 5) # @new has the removed elements 0-4
splice (@x, 2, 1, "dog") # element 2 replaced with "dog"
@new = @x[1,4,6] # @new has only elements 1,4,6
($a,$b) = @x # assigns $a/$b from array
Regardless of the way in which the array is created, each array element
can is a single value, consisting of a string, integer, or floating point number.
Array elements do not have to be the same data type.
There are two basic ways to walk through an array - using for or foreach loops.
The following code show both approaches, along with a few manipulation
tips on arrays.
for ($i=0, $i<10, $i++) { print $myarray[$i] ) # for loop
foreach $var ($myarray) { print $var } # foreach loop
Hash
A hash consists of key/value pairs and can be created in several ways
%x = ("a",1,"b",2,"c",3) # assign a list of pair values
%x = ("a"=>1,"b"=>2,"c"=>3) # comma may be replaced with =>
$x{"a"}=1 $x{"b"}=2 $x{"c"}=3 # manually add key/value pairs
Regardless of the way in which the hash is created, each hash key and
each hash value consist of a single a string, integer, or floating point
number. Hash keys do not have to be the same data type. Likewise,
hash values do not have to be the same data type.
To walk through a hash, use the keys function to get a list of the hash
keys for use in a foreach loop. Or, use the each function, which returns
a key/value pair each time it is called. The following code shows both
approaches, along with a few manipulation tips on hashes.
foreach $key (keys %myhash) { print "$key: $hash{$key}\n" }
while (($key,$value) = each (%myhash)) { print "$key: $value\n" }
%myhash = () # clear a hash
delete ($myhash{$key}) # remove key/value pair
if exists $myhash{$key} # test for existence of a key
clear
key exists
delete key/value pair
If you have any suggestions for additions to these tutorials, please let me know.
|