|
Scalar variables
All scarlar variables
- start with the "$" symbol and a letter
- + other letters, numbers or underscore.
- The contents can contain 1 to many characters, numbers or other special characters.
- Upper vs lower case letters represent different variable names. $Valueand
$value represent two different variables.
You can assign the contents to a scalar variable by...
$x = "value";
$x = "can contain a phrase";
$x = <STDIN>;
# takes input from keyboard
top of page
Special values
Special values must be proceeding by a backslash (\) in order to be include with
in the contents of scalar variable. The following table is a list of such values.
| Construct |
Meaning |
Construct |
Meaning |
| \n |
Newline |
\r |
Return |
| \t |
Tab |
\f |
Formfeed |
| \b |
Backspace |
\a |
Bell |
| \e |
Escape |
\007 |
Any octal ASCII value |
| \\ |
Backslash |
\x7f |
Any hex ASCII value |
| \" |
Double quote |
\cC |
Any control character (Control C) |
| \l |
Next letter lower case |
\L |
Lowercase all letters until \E |
| \u |
Next letter upper case |
\U |
Uppercase all letters until \E |
| \Q |
Backslash-quote all non-alphanumerics until \E |
\E |
Terminate \L, \U or \Q |
top of page
Array variables
All array variables
- start with an "@"
- Contain a "list" or "elements"
- Each element is a separate 'scalar' variable.
You can assign the contents to a array by...
@x = ("value1", "value2", "many words")
You can refer to each element of the array variable individual by using the
"$" scalar symbol and an element number in '[ ]'s.
value of $x[0] is 'value1'
value of $x[1] is 'value 2'
value of $x[2] is 'many words'
$x[4] = "new entry"; # sets the 4th element to 'new entry'
top of page
Length
To get the length of all elements in an array, let a scalar
variable = array variable. @name = ("roger", "petersen");
$namesize = @name;
# value of $namesize = 14
($namesize) = @name;
# value of $namesize = 'roger'.
# Set to first element in the array
top of page
Number of Elements
To determine the number of elements in an array use '$#'
@x = ("value1", "value2", "many words");
$numelements = $#x;
# $numelements = 2, remember count starts at 0
top of page
push and pop
push and pop are used to add or subtract
elements from the right side of an array.
@x = (1, 2, 3);
push(@x, "4");
# adds new element '4'. @x = 1, 2, 3, 4
$last = pop(@x);
# $last =4, @x = 1, 2, 3
top of page
unshift and shift
unshift and shift are used to add or subtract elements from the left side.
@x = (4, 5, 6);
unshift(@x, 1,2,3);
# @x = 1,2,3,4,5,6
$firstvalue = shift(@x);
# $firstvalue = 1, @x = 2,3,4,5,6
top of page
reverse
reverse is used to reverse the order of all elements in an array.
@x = (1, 2, 3, 4, 5);
@x = reverse(@x); # @x = 5,4,3,2,1
top of page
sort
sort is used to return the sorted values of all elements
@x = (1, 2, 4, 8, 16, 32, 64);
@x = sort(@x); # @x = 1,16, 2, 32,4, 68, 8
top of page
Hash variables
All hash variables
- start with the '%' symbol.
- Hash variables are similar to arrays with the extra feature of having a 'reference key'.
You assign the contents to a hash variable in pairs...
%mydata=("key1","value1","key2","value2","key3","data
associated with key3")
Just like arrays, each key and associated data element can be referenced
by using scalar variables. The key value is enclosed with '{ }'.
$key="key1";
$data = $mydata{$key1};
#$data now contains 'value1'
$data =$mydata{"key3"};
#$data now contains 'data associated with key3'
Another method for assignment is...
$zipcode("60126") = "Elmhurst, IL";
# creates key 'of 60126' with a value of 'Elmhurst, IL'
print $zipcode{"60126"};
# prints the value for the key, i.e. 'Elmhurst, IL'
$var(123) = "456.7"
# creates a key of '123' with a value of '456.7'
$var(123} += 3;
# increase the value of key '123' by 3, i.e. value = '459.7'
top of page
keys and values
The keys function only returns all of the keys for a hashed variable. The
values function only returns all of values for a hased variable.
$zipcode("60126") = "Elmhurst, IL";
$zipcode("60016") = "Des Plaines, IL";
@zips = keys(%zipcode);
# @zips contains 60126, 60016
@cities = values(%zipcode);
# @cities contains 'Elmhurst, IL' and 'Des Plaines, IL'
top of page
each
The each function is used to access all elements of the entire hash in pairs.
while (($zip, $city) = each(%zipcode)) {
print "The city for zip code $zip is $city\n";
}
top of page
delete
The delete function is used to delete a specified key and value of a has variable.
delete $zipcode{"60126"};
# deletes the key 60126 and the value 'Elmhurst, IL'
top of page
|