【发布时间】:2014-04-15 12:51:13
【问题描述】:
我将我的函数中的哈希设置为
$hash{$loginId}{$month}=$hash{$loginId}{$month}+$time1;
我可以将值打印在一个位置
print "$hash{maclawty796}{Sep}\n"; #just some name and date in the text file i imported
但我想打印出所有的值。 当我使用
print "@{[%hash]}\n";
我得到了一堆奇怪的结果,看起来像是内存地址。如何打印 loginId 的月份和时间?
如果你需要,这里是完整的代码
#
# adapted from regex9.pl
#
$file ='timelog.txt';
open(INFO, $file) or die("Could not open file.");
use strict;
my ($loginId, $month, $time1, $time2, $pts, $line, $time1T, $time2T);
my %hash=();
while (<INFO>)
{
$line=$_;
#assignment of Login ID's
if( ($loginId) = /([a-y]*(\d|\w)*)/ ){
printf("%-15s", $loginId);
}
if( ($month) = /((Jan)|(Fed)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))/ ){
printf("%-5s", $month);
}
if(($time1, $time1T, $time2,$time2T)= /(\d\d):(\d\d)\s-\s(\d\d):(\d\d)/ ){
#print($time1, " ", $time1T, " ", $time2," ", $time2T,"\n"); #for testing
$time1=($time2*60+$time2T)-($time1*60+$time1T);
printf ("%-5s minutes\n" ,$time1);
}
$hash{$loginId}{$month}=$hash{$loginId}{$month}+$time1;
print "hash $hash{$loginId}{$month}\n";
}
print "@{[%hash]}\n";
print "$hash{maclawty796}{Sep}\n";
【问题讨论】:
-
maclawty796 pts/1 10.1.28.122 Mon Oct 24 09:18 - 09:20 就像我的文本文件中的内容