zulqernain has asked for the wisdom of the Perl Monks concerning the following question:

hi i have some repeated strings in an array and i want to save them in a hash with the value be the number of times it epeated in the array .can you tell me how to do it i made an array
@array=("abc","def","abc","klm","abc","rbc","Kjm","rbc","abc");
when i put it in hash
for $i (@array) { $hash{$i}=1; }
it put the unique keys in hash with value 1 each . i dont know what i should write instaed of 1 so that it save the count as value corrsponding to each key

Replies are listed 'Best First'.
Re: hash and array
by Tanktalus (Canon) on May 22, 2005 at 14:59 UTC

    So very close. :-)

    Change the "=1" to "++".

    for my $i (@array) { $hash{$i}++; }
      thanks
Re: hash and array
by ivancho (Hermit) on May 22, 2005 at 15:15 UTC
    it would have been funky to be able to say ++(@hash{@array});
    closest to that, $_++ for @hash{@array}; works
      (Update: Verified.) The Perl 6 code for that be %hash{@array} >>++; which I think is just freaking awesome.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

        What does the expression %hash{@array} >>++ evaluate to in list context? With sigils just part of the name, I don't have any feel for this yet. But I suppose the evaluation is a property of the >>++ op.

        After Compline,
        Zaxo

        just freaking awesome
        beep poop poop beep poop poop beeeep. The results are in, it is freaking awsome :)