Hashes are just like arrays, but instead of numbers, hashes use strings as the index.
Declaration 1:
%longday = ("sun" => "sunday",
"mon" => "monday",
"tue" => "tuesday",
"wed" => "wednesday",
"thu" => "thursday",
"fri" => "friday",
"sat" => "saturday",
);
arrays use @, while hashes use %
the left hand side are the keys, the right hand side are the values.
Another way of populating hashes:
%month = ("jan","january","feb","february","mar","march");
the first character of each pair of strings is the key, while the subsequent is the value.
We can also create a hash just by creating a pair value:
$dog{'jrt'} = "Jack Russel Terrier";
Accessing a single hash value:
print $dog{'jrt'};
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Dec 27, 2006
Nov 23, 2006
Basic #7, Arrays
Assignment:
@animals = ('cats','dogs','rabbits');
Retrieval:
$animals[2] -> returns rabbits
counting starts from 0
Length:
$sum = @animals;
print $sum;
prints 3;
Last Index:
$#animals -> returns 2
Printing:
print @animals -> prints catsdogsrabbits
print "@animals" -> prints cats dogs rabbits
Adding:
push(@animals, "mouse"); -> adds "mouse" to the list
$marsupial = 'kangaroo';
push(@animals, $marsupial); -> adds "kangaroo" to the list
@predators = ("tigers","leopards");
push(@animals, @predators); -> adds "tigers" and "leopards" to the list
Remove last value:
$cute = pop(@animals); -> remove the last item from the list, and assigns it to $cute
@animals = ('cats','dogs','rabbits');
Retrieval:
$animals[2] -> returns rabbits
counting starts from 0
Length:
$sum = @animals;
print $sum;
prints 3;
Last Index:
$#animals -> returns 2
Printing:
print @animals -> prints catsdogsrabbits
print "@animals" -> prints cats dogs rabbits
Adding:
push(@animals, "mouse"); -> adds "mouse" to the list
$marsupial = 'kangaroo';
push(@animals, $marsupial); -> adds "kangaroo" to the list
@predators = ("tigers","leopards");
push(@animals, @predators); -> adds "tigers" and "leopards" to the list
Remove last value:
$cute = pop(@animals); -> remove the last item from the list, and assigns it to $cute
Subscribe to:
Posts (Atom)