ARRAYS
@array = (1,2,3);
print $array[0]; #-> 1
@array = (
"one", "two", "three",
"four", "five", "six"
);
@array = (0) x 100;
use of the x operator for an array
print $#array; #-> 99
@array = qw(one two three)
use of the qw quote operator
@new = reverse @array
reverses @array
@new = sort{$a<=>$b} @array
sorts @array
$#array
last index in array. Synonymous with array.length
for(var $i=0;i<$#array[)];i++)
$#array+1 = scalar(@array)
is same as scalar(@array)
$var = @array; print $var
print "There are scalar(@array) elements in the array.\n";
push ARRAY, LIST
Adds LIST array to end of ARRAY array.
push(@array,"one");
push(@array,"two");
print $array[1]; #-> two
pop ARRAY
Removes and returns last value of ARRAY.
@array = ("one","two");
$var = pop(@array);
print $var; #-> two
shift ARRAY
removes and returns first value of ARRAY
unshift ARRAY, LIST
adds LIST array to front of ARRAY array.
To empty an array, set it to a negative number:
$#array = -1;
Merging and Splicing Arrays:
@array1 = (1,2,3);
@array2 = (4,5,6);
@array3 = (@array1,@array2);
@array1 = (1,2,3,4,5,6);
@array2 = @array1[2..3];
print join(", ", @array2); #-> 3, 4
split ARRAY, OFFSET [, LENGTH] [, LIST]
w/no LENGTH, splice removes everything past OFFSET
and appends to LIST or $_.
Array for and foreach loops:
foreach loop is a simplified for loop:
for($i=0; i<@array; i++) {}
foreach $i (@array) {}
using $_ with for loops:
for(@array) {print;}
#----------------------------------------------------------------------------------
HASHS
%hast = ();
empty hash (not necessary)
$hash{fruit} = "apple";
"$" scalar assignment
%hash = (%hash, dressing, 'blue chesse');
add to hash
if(exists($hash{"veg"})) {
print "the veg hash exists";
}
delete($hash{"veg"});
deletes veg hash
%hash1 = (
fruit, apple
sandwich, ham_cheese
);
%hash2 = (
fruit => apple
sandwich => ham_cheese
);
%hash3 = (%hash1, %hash2);
($var1, $var2, @array) = (1,2,3,4,5,6,7,8);
print $var1; #-> 1
print $var2; #-> 2
print "@array\n"; #-> 3 4 5 6 7 8
print "@{[%hash]}\n";
while(($key, $value) = each(%hash)) {
print "$key => $value\n";
}
foreach $key (keys %hash) {
# using the keys function:
print $hash{$key} . "\n";
}
foreach $value (values %hash) {
# using the values function:
print "$value\n";
}
foreach $key (sort keys %hash) {
# using the sort and keys functions:
print "$key => $hash{$key}\n";
}
foreach $value (sort values %hash) {
# using the sort and values functions:
print "$value\n";
}
#----------------------------------------------------------------------------------
TYPEGLOBS
Typeglobs work like aliases in Perl. What they actually do is copy the complete
symbol table entry for a name into the symbol table entry for a new name.
You can consider the "*" typeglob a wildcard for all data types ($, @, %).
$data = "This is a line of data.";
@data = (1,2,3);
# begin typeglob assigment->
*alsodata = *data;
# <- end typeglob assignent
print "$alsodata"; #-> This is a line of data.
print "@alsodata"; #-> 123
If you only want to copy the entire symbol table entry for a namespace when
creating a typeglob, you can use a reference only to the data type you need.
*alsodata = \$data;
Perl stores the names of your variables in a symbol table, and each symbol
table entry is a typeglob. You can think of typeglobs as hashes whose values
are references to the actual data in your variables. The keys in these hashes,
written in uppercase, correspond the various possible data types like SCALAR,
ARRAY, HASH, etc.. You can pick apart the Perl symbol table directly if you
want to experiment with this.
$var = 5;
print ${*var{SCALAR}}; #-> 5