作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的函数中将我的哈希设置为
$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";
最佳答案
%hash
中的值是对其他哈希值的引用。如果您想打印这些散列的键和值,则必须指示 Perl 这样做。
for my $loginId (keys(%hash)) {
for my $month (keys(%{ $hash{$loginID} })) {
print("$loginID $month $hash{$loginID}{$month}\n");
}
}
关于perl 如何用多个键打印出一个散列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22333699/
我是一名优秀的程序员,十分优秀!