问题
working in perl I have managed to write a program that reads from a file usernames. Each username has logged in and out and these logs have been recorded. For example:
maclawty796 pts/1 ip-64-134-238-2. Fri Oct 21 14:23 - 14:25
maclawty796 pts/2 10.1.28.122 Fri Oct 21 09:42 - 09:55
ehowe pts/3 10.1.28.204 Fri Oct 21 09:28 - 09:29
As you can see, the users may be logged twice. I am trying to not unify the information of all these accounts by username and month. For example:
maclawty796 Oct xxx minutes
maclawty796 Sep xxx minutes
I've managed to get the individual times and logs, see below for the code, and now I assume to unify the times and dates, I'd have to use a hash or hash of hashes. I have a feeling that my hash will have to be username then a hash of the two months that the username was active and within that hash another hash to add all the time that user was logged in in that month up. Then I'd have to output it. Problem is, I don't know where to start the hash. In the code you can see my futile attempt at making a basic hash as well.
#!/usr/bin/perl
use strict;
my($name, $month, $time1, $time2, $time3, $time4, $totstime, $totstime2, $totaltime);
my %gash = (
username => $name,
);
open (MYFILE, 'Textlog.txt');
while (<MYFILE>){
if(($name, $month, $time1, $time2, $time3, $time4) = /(\w+\d*?).*(Oct|Sep).*(\d\d).*(\d\d).*(\d\d).*(\d\d)/){
chomp;
$totstime = ($time3 - $time1)*60;
$totstime2 = $time4 - $time2;
$totaltime = $totstime + $totstime2;
print "name $name month $month $totaltime\n";
}
}
close(MYFILE);
来源:https://stackoverflow.com/questions/22447763/perl-hash-could-be-a-hash-of-hashes