Hashes
A hash is an un-ordered group of KEY-VALUE pairs and keys are unique strings(duplication of keys are not allowed). The values are scalar and they can be either a number ,a string or a reference. These are also called as associative arrays.
Before using the hash we have to first declare it
my %hash ;
As I said above duplication of keys are not allowed and this property can be used to list out the elements without any repetition. Let us consider a array of numbers with repetition.@numbers =(0,2,3,4,1,4,2,2,3,5,5)
my @unique;
my %hashes;
my @numbers =(0,2,3,4,1,4,2,2,3,5,5);
foreach my $value (@numbers) {
if (! $hashes{$value}) {
push @unique, $value; $hashes{$value} = 1;
}}
print @unique;
output:023415
No comments:
Post a Comment