Perl hash (could be a hash of hashes)

Deadly 提交于 2019-12-25 06:54:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!