Académique Documents
Professionnel Documents
Culture Documents
Language
PERL
Arrays & Hashes
Index: $variableName[index]
Index starts at 0 just like in C
# Output: three
shift and unshift do to the left end of the array with push
and pop do to the right end.
The shift function shifts the first value of the array off and
returns it, shortening the array by one element and moving
everything down one place
shift ARRAY
This unshift function does the opposite of shift; it adds LIST
to the front of the array and returns the new elements in
the array.
unshift ARRAY, LIST
Copyright Amstar Technologies
10
11
12
print $array[$loop_index];
}
Output: onetwothree
5
13
5
14
Printing Arrays
When you just want to print an array, you can
pass it to the print function this way
@array = (one, two, three);
print Here is the array: @array.\n;
Output: Here is the array: one two three.
Copyright Amstar Technologies
15
16
17
18
Sorting Arrays
To sort an array, you use the sort function.
@new = sort {$a <=> $b} @array;
Heres an array sorted in descending order.
@new = sort {$b <=> $a} @array;
Copyright Amstar Technologies
19
Index: $varName{key};
20
apple,
hamburger,
bubbly,
print $hash{fruit}\n;
# Output: apple
21
);
fruit
sandwich
drink
=>
=>
=>
apple,
hamburger,
bubbly,
print $hash{fruit}\n;
# Output: apple
# Output: pecan
22
23
24
25
apple
26
27
28
hashes,
we
can
use
list
$hash1{fruit} = apple;
$hash1{sandwich} = hamburger;
$hash1{drink} = bubbly;
$hash2{cake} = chocolate;
$hash2{pie} = blueberry;
$hash2{ice cream} = pecan;
%bighash = (%hash1, %hash2);
print $bighash{ice cream};
Output: pecan
Copyright Amstar Technologies
29
30