hash - Perl: Get key value -


i'm trying key values hash inside module:

module.pm

... $logins_dump = "tmp/logins-output.txt"; system("cat /var/log/secure | grep -n -e 'accepted password for' > $logins_dump"); open (my $fh, "<", $logins_dump) or die "could not open file '$logins_dump': $!";  sub userlogins {     %user_logins;     while (my $array = <$fh>) {         if ($array =~ /accepted\s+password\s+for\s+(\s+)/) {             $user_logins{$1}++;          }     }     return \%user_logins; }  sub checkuserlogins {     $logincounter;     $userstocheck = shift @_;     if (exists %{userlogins()}{$userstocheck}){         $logincounter = %{userlogins{$userstocheck}}; #how many logins?     }     else {         $logincounter = "0";     }     return \$logincounter; } 

script.pl

$userlogincounter = module::checkuserlogins($userstopass); 

i pass usernames script , check if username in hash, if is, need return number of logins, i'm trying $logincounter. reason scripts returns 0 or undef.

well, starters - you've got checkuserlogins not checkloginattempts.

assuming that's typo - userlogins returns hash reference - single scalar value. you're getting 0 if exists check fails presumably.

if does exist though, you're doing this:

$logincounter = %{userlogins{$userstocheck}}; 

which isn't valid. have strict , warnings turned on? because you're trying assign hash scalar, isn't going want.

you mean:

$logincounter = ${userlogins()} -> {$userstocheck}; 

which dereferences reference userlogins , looks key.

i might however, approach problem little differently - it'll work once when you're doing, because each time call userlogins creates new hash, don't rewind $fh.

so i'd suggest:

use strict; use warnings;  {     %userlogins;      sub inituserlogins {         open( $fh, "<", '/var/log/secure' )             or die "could not open file: $!";         while ( $array = <$fh> ) {             if ( $array =~ /accepted\s+password\s+for\s+(\s+)/ ) {                 $userlogins{$1}++;             }         }         close($fh);     }      sub checkuserlogins {         ($userstocheck) = @_;         inituserlogins() unless %userlogins;         return $userlogins{$userstocheck} ? $userlogins{$userstocheck} : 0;     } } 

Comments

  1. @admin

    We provide all the requirements around the patients to give friendly involvement with divine and relaxed circumstances. Book an appointment and visit our clinic for our effective doctor’s experiences

    Regards,
    Dentist In Dracut, MA

    ReplyDelete
  2. @admin

    Our team takes care about dental care with excellent equipment and material in your comfortable atmosphere. Your comfort is our priority, which suits your smile as per your requirement.

    Regards,
    Dentist In Lynn MA

    ReplyDelete
  3. @admin

    Our Center uses the latest and advanced technology for our patient treatment, so that the relationship of trust never gets cracks. At Affordable Dental Care, we acquire a strong culture of patient safety by our dental specialists.

    Regards,
    Dentist In Dracut, MA

    ReplyDelete

Post a Comment

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -