Perl array , hashes example

#!/usr/bin/perl
#here is something interesting to calculate occurence of same word in an array

#lets start
# lets make a simple array , in perl for array @ is used

my @words = (“I”,”love”,”perl”,”programming”,”I”);

#to get the length of the array, simply equate it to a variable
$no_of_words = @words; #this will give u 5 (length)

#now make some hashes (hashes are simple associative array with key and value)
%occurence_of_word = ();

for my $i(1..$no_of_words)
{
$occurence_of_word{$words[$i-1]}++;
}
#print($occurence_of_word{“I”}); // this will give you 2

for my $wordss(keys %occurence_of_word)
{
print “$wordss \t”.” $occurence_of_word{$wordss}\n”; # this will give u the number of occurences of words

}

Explore posts in the same categories: perl

Comment: