gpt4 book ai didi

perl 如何用多个键打印出一个散列?

转载 作者:行者123 更新时间:2023-12-02 04:34:11 29 4
gpt4 key购买 nike

我在我的函数中将我的哈希设置为

$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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com