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

I did check previous post about "Looking Through Arrays?", and whatever I tried did not work
I created an array from a text file with passwords
HHYYTT
TTRRDD
PPLLOO...
open(PFILE, "Activatepasswords.txt")|| die "Cannot open patterns1.txt +file"; my @PatternArray=<PFILE>; close(PFILE); pop @PatternArray if $PatternArray[-1] eq "\n"; @PatternArray = grep /\s/, @PatternArray;
I want to count how many times specific passwordKey ocurrs, only if it match passwords from previous array , the following is what I have been trying..
my $passwordKey = $pass1; if (grep($_ eq $passwordKey, @PasswordsArray)){ print "ok\n"; my $count = $passwordKey; $frequency{$count}++; }
or

my %PassValues; @PassValues{@PasswordsArray}=(); if (exists $PassValues{$passwordKey}){ print "count\n"; my $count = $passwordKey; $frequency{$count}++; } else { print "Not count\n";; }
After this I print the hash with freq, but I am not able to get any result or match.
Any idea?
Thanks

Desperada

Replies are listed 'Best First'.
Re: Matching values in array
by kvale (Monsignor) on Mar 02, 2004 at 05:28 UTC
    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

      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.
Re: Matching values in array
by QM (Parson) on Mar 02, 2004 at 05:10 UTC
    How do you print the hash with freq?

    Why don't you show us more code, as it's hard to tell if you've gone wrong in the loop you left off.

    ...On further review, your code needs scrubbing. For instance, you have

    open(PFILE, "Activatepasswords.txt")|| die "Cannot open patterns1.txt +file";
    If the open fails, your error message reports on a different file?

    You'll get better answers if you clearly state the problem, and reduce the code to the minimal set that repeats the symptoms.

    Sorry I couldn't be more helpful, it's bobos for me.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Sorry about that, the thing is that I am working with different scripts trying to figure it out what is going wrong, that's why the code needs "scrubbing" like you said

      I don't know what else to give you, my problem is that I don't get to match my $passwordKey with any ofthe passwords from the array. I did double check space or blank in the string in case that was the reason of not matching, but nothing!

      Here how I print the freq, which is not my main problem, it runs well in other script
      foreach $key (sort by_score keys %frequency){ print OUTFILE "$key $frequency{$key}\n"; } sub by_score { $frequency{$b} <=> $frequency{$a}; }
Re: Matching values in array
by Roger (Parson) on Mar 02, 2004 at 05:51 UTC
    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.

      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;

Re: Matching values in array
by Anonymous Monk on Mar 02, 2004 at 05:04 UTC
    Note: The first array created with passwords from a text file is called @PasswordArray not @PatternArray, sorry about that. Any way I still have the same problem not matching!. Thanks in advance
Re: Matching values in array
by TomDLux (Vicar) on Mar 02, 2004 at 17:42 UTC

    You ask about a very specific Perl problem, but I am dubious about the application you are trying to achieve.

    Secure and Unsecure Passwords

    What is this situation where you have clear-text passwords? Do you consider this a secure situation?

    • If you are dealing with passwords, it's much better to store the encoded version than the cleartext version. That makes it harder for someone who obtains access to the file/DB to steal passwords.
    • Secure encryption methods involve some form of randomization, commonly referred to as salt. Without salt, BadGuy can encrypt bad_pw and compare the encrypted form with your list of passwords. A match means someone was using bad_pw as their password. With salt, which can take on, say, one of a thousand values, BadGuy needs to encrypt each potential password a thousand times to determine whether it appears in your list, slowing him down.
    • Prevent people from accessing the encrypted passwords. Someone who has the list of encrypted passwords on his own machine can try hundreds or thousands of common passwords a second against the list. Unsuccessful attempts to log in to your machine should invoke a one second delay. This inconveniencecs users only minimally, but prevents villians from testing more that 3600 passwords an hour, 86400 passwords a day. Of course, they could use several machines to test passwords, so you'll need to detect such distributed attacks and limit the number of threads which will respond.

    Monitoring Passwords

    Depending on the situation, it's good to test passwords for a certain standard, at the time the user wants to set his/her password. Check for common words, variations on the user's account name, etc. But testing should be appropriate to the circumstance in which it is used. If you are runnning a bank, it's suitable to insist users have secure passwords which are changed regularly. If you are providing a pay web site, the only problem with someone hacking a customer's login is that they are obtaining free access to your service. But the odds are they will distribute that login information, and you'll detect the site slowing down as hundreds of users connect, at which point you can disable that account to protect your site. The customer will eventually be in touch, at which point he can receive the standard lecture on insecure passwords.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA