in reply to Matching values in array

The following code counts how many times a given password occurs in a list:
my $passwdkey = 'bob'; my $passcount = 0; while (<DATA>) { chomp; if ($_ eq $passwdkey) { $passcount++; print "ok\n"; } } print "final '$passwdkey' count = $passcount\n"; __DATA__ ted bob carol alice bob hank
A fancier way to do this is to use grep:
my $passcount = grep {$_ eq "$passwdkey\n") <DATA>;

-Mark

Replies are listed 'Best First'.
Re: Re: Matching values in array
by Anonymous Monk on Mar 02, 2004 at 05:47 UTC
    Thanks monk, but one question... Are you suggesting that I should check/look my passwdKey using my textfile not the array?, ... I have problems with the matching process not the counting
    For the counting as I put in my code, I need a hash that keeps the passwdKey and the count, and then print them.