On Wed, Apr 17, 2013 at 1:17 PM, Irfan Sayed <irfan_sayed2...@yahoo.com>wrote:

> please suggest


You need to work on indenting!  Name a hash and an array the same is going
to lead to troubles down the road. The standard idiom for going through a
hash is to just get the key and the ref the value via the hash:
foreach my $key ( keys %names ) {
  print "$key => $names{$key}\n";

as long as you're looping, instead of debug printing just "hi"
print "new key $a\n"
...
print "found key $key"

don't use $a and $b as they are special vars for use in the sort function's
anonymous subs.  Your loop could actually just be:
$names{$_}++ foreach @words;

Your data is there, but your final for loop is wrong
foreach ( ($key2, $value2) = each %names ) {

"each" returns a list of pairs and as you have an list context:
($key2, $value2)

on the LHS of the "=", they get assigned the first two elements of the list
"each' produces:
c, 1, b,1, 1,2

(note the order is arbitrary and can't be counted on) and the rest gets
thrown away.  So you only see one loop of printing - you wanted a while
loop:
while ( ($key2, $value2) = each %names ) {

and the better idiom is;
foreach my $key ( keys %names ) {
    print "got: $key => $names{$key}\n"
}

got: c => 1
got: a => 2
got: b => 1

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to