Hi samjesse,
using a for loop to iterate the array @subjects and at each instances,
count the number of occurrence of each *member* [or element] of the array
@subjects, [ and STORE] using a hash %subjects [ which of course is
different from the array @subjects, though
the same name was used ]. The auto-increment "++", is used here to
increase [ or update], the number of occurrence of the array element, if
such is available.
Please NOTE: 1. Am sure that both $msg and %subjects are declared,
somewhere in your code.
2. Use different names or [ naming ] for your variables
[ as in the case of @subjects and %subjects ], it will help clean codes and
less confusion for you later or others -- of course not for perl!
Am sure others will mention use of "strict and warnings", which indeed
is a good practice!
<codes>
#!/usr/bin/perl -w
use strict;
my @subjects=qw{maths english physics chemistry maths futhermaths};
my %check_subject;
foreach my $msg (@subjects) {
$check_subject{$msg}++;
}
while(my($key,$value)=each %check_subject){
print $key," ==> ",$value,"\n";
}
</codes>