in reply to Matching values in array

use strict; use warnings; use Data::Dumper; chomp(my @PatternArray = map { /^\s*$/ ? () : $_ } <DATA>); my %occurance; $occurance{$_}++ for @PatternArray; print Dumper(\@PatternArray), Dumper(\%occurance); # using occurance table print "PPLLOO has appeared $occurance{PPLLOO} times.\n"; # using grep print "TTRRDD has appeared ", scalar grep(m/TTRRDD/, @PatternArray), " times.\n"; __DATA__ PPLLOO HHYYTT TTRRDD PPLLOO TTRRDD PPLLOO PPLLOO

And the output is:
$VAR1 = [ 'PPLLOO', 'HHYYTT', 'TTRRDD', 'PPLLOO', 'TTRRDD', 'PPLLOO', 'PPLLOO' ]; $VAR1 = { 'TTRRDD' => 2, 'PPLLOO' => 4, 'HHYYTT' => 1 }; PPLLOO has appeared 4 times. TTRRDD has appeared 2 times.

Replies are listed 'Best First'.
Re: Re: Matching values in array
by Anonymous Monk on Mar 02, 2004 at 06:13 UTC
    Thanks for answering,but my problem is that I will count the occurance ONLY If the referred passwordKey match the passwd in DATA... maybe I was not too clear in my message sorry
      What's wrong with grep?
      print "XXXXXX has appeared ", scalar grep(m/XXXXXX/, @PatternArray), " times.\n"; ... my $count = scalar grep m/XXXXXX/, @PatternArray;

        It does not work with grep, why?.. who knows. The matching is working now, I use your
        chomp(my @PatternArray = map { /^\s*$/ ? () : $_ } <DATA>);
        to read my passwd from the text to the array and I using the following to match
        my %PassValues; @PassValues{@PasswordsArray}=(); if (exists $PassValues{$passwordKey}){ my $count = $passwordKey; $frequency{$count}++; }