【问题标题】:perl how to print out a hash with multiple keys?perl如何打印出具有多个键的哈希?
【发布时间】: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 就像我的文本文件中的内容

标签: perl hash printing


【解决方案1】:

%hash 中的值是对其他哈希的引用。如果要打印这些哈希的键和值,则必须指示 Perl 这样做。

for my $loginId (keys(%hash)) {
   for my $month (keys(%{ $hash{$loginID} })) {
      print("$loginID $month $hash{$loginID}{$month}\n");
   }
}

【讨论】:

    【解决方案2】:

    哈希不能有多个键。一个散列只有一层键,但这些键的值可以指向其他散列。

    您有一个名为%hash 的散列,由$loginId 键入。此散列中的值指向其他散列(每个值都是一个完全独立的散列),它们都由$month 键入。理解这一点很重要。

    我发现使用-&gt; 运算符可以帮助自己记住这一点。这让我想起了我在谈论 references 而不仅仅是其他键。

    例如,不要这样写:

    $hash{$loginId}{$month} = $hash{$loginId}{$month} + $time1;
    

    我会这样写的:

    $hash{$loginId}->{$month} = $hash{$loginId}->{$month} + $time1;
    

    这并不多,但这是一个很好的提醒,$month哈希引用 的键,我需要取消引用该哈希,然后才能看到它的值。

    让我们来一个简单的循环,打印出%hash

    for my $loginId ( sort keys %hash ) {
        print qq(\$hash{$loginId} = "$hash{$loginId}"\n";
    }
    

    这将打印出如下内容:

    $hash{bob} = "HASH(0x7f8e3b804ee8)"
    $hash{david} = "HASH(0x...)"
    $hash{sue} = "HASH(0x...)"
    

    HASH{0x...) 是哈希引用的地址。我需要取消引用该哈希以获取值:

    for my $loginId ( sort keys %hash ) {
        my %months = %{ $hash{$loginId) };  # Dereferencing the hash;
    }
    

    现在,我已经取消引用了哈希,我可以打印出每个用户的月份:

    for my $loginId ( sort keys %hash ) {
        my %months = %{ $hash{$loginId) };
        for my $month ( sort keys %months ) {
            print qq(\$hash{$loginId}->{$month} = ) . $hash{$loginId}->{$month} . "\n";
        }
    }
    

    顺便说一句,我没有理由不能将引用与内部 for 循环结合起来:

    for my $loginId ( sort keys %hash ) {
        for my $month ( sort keys %{ $hash{$loginId) } ) {
            print qq(\$hash{$loginId}->{$month} = ) . $hash{$loginId}->{$month} . "\n";
        }
    }
    

    您似乎正在尝试快速转储所有数据。为此,您可以使用Data::Dumper 模块:

    use strict;
    use warnings;
    use feature qw(say);  # Better way to print than print
    
    use Data::Dumper;
    
    ....
    say Dumper \%hash;  #Prints out the entire data structure.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      相关资源
      最近更新 更多