Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building array need help

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
Ok so I have a script that I am writing that pulls lines from a file into an array. I am then cycling through that array and looking for lines that say LOGIN. From those lines I am pulling the date (it is a log file) and the userid and placing them into variables $zdate and $zuser. What I want to do now is place those two variables into an array for sorting. Once i have them in an array, I will sort through and delete multiple instances of $zuser.. Since a user is likely to log on more than once per day, there will be several elements containing the same userid. I want my array to only show the first instance.. What I am planning on doing is taking this array, and updating an MySQL table with the last date of login for that userid.
here is what I have so far

Code:
##
## Search mail log for expression LOGIN
##
open(MAIL_LOG, $MAIL_LOG) || die("Could not open MAIL_LOG");
@mail_line=<MAIL_LOG>;
close MAIL_LOG;

$search = " LOGIN";
foreach $mail_line(@new_mail_line){
 if($mail_line=~/$search/){
  $zmonth = $months{substr($mail_line,0,3)};
  $zday = substr($mail_line,4,2);
  $zdate = substr($mail_line,0,6);
  my($zuser) = ($mail_line =~ /user=([^@]+)@/);
  @zdate = $zdate;
  @zuser = $zuser;
  @update = (\@zdate,\@zuser);
 }
}

Where I am getting hung up at is putting the array together then writing it to a file the entries in my file just say something like: Array(0x830bc68)
 
Don't use an array, use a hash. Then you won't have to worry about de-duping it and sorting it. Something like
Perl:
my %logins;
..
..
$logins{$user} = $date;
..
..

foreach my $u (sort keys %logins) {
   print "UPDATE myTab SET last_update = '$logins{$u}'
      WHERE userid = '$u';\n";
}
Not tested, but you get the idea...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hey thanks, that worked fabulously!! All my programming projects here at work are done in languages I have not learned yet so I have to dive right in, thank the heavens for people on here who are experienced.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top