【问题标题】:Why does my multi-leveled hash print the way I expect?为什么我的多级哈希打印方式符合我的预期?
【发布时间】:2011-08-18 15:55:31
【问题描述】:

这是代码,但它不起作用,我想做的是将哈希的哈希传递给子例程又名函数,但它给出了一些奇怪的输出。

my %file_attachments = (
         'test1.zip'  => { 'price' => '10.00', 'desc' => 'the 1st test'},
         'test2.zip'  => { 'price' => '12.00', 'desc' => 'the 2nd test'},
         'test3.zip'  => { 'price' => '13.00', 'desc' => 'the 3rd test'},
         'test4.zip'  => { 'price' => '14.00', 'desc' => 'the 4th test'}
                   );

                   my $a="test5.zip";
                   my $b="the 5th test";

         $file_attachments{$a}->{'price'} = '18.00';
         $file_attachments{$a}->{'desc'} =$b;


        print(%file_attachments);


sub print{

my %file =@_;

foreach my $line (keys %file) {
        print "$line: \n";
         foreach my $elem (keys %{$file{$line}}) {
          print "  $elem: " . $file{$line}->{$elem} . "\n";
    }
                 }

输出::::

      test2.zipHASH(0x3a9c6c)test5.zipHASH(0x1c8b17c)test3.zipHASH(0x1c8b3dc)test1.zipHASH(0x3a9b1c)test4.zipHASH(0x1c8b5dc)   

【问题讨论】:

    标签: perl hash hash-of-hashes


    【解决方案1】:

    perlcritic 可以成为调试 Perl 代码的便捷工具:

    perlcritic -1 my_code.pl
    
    Subroutine name is a homonym for builtin function at line 24, column 1.  See page 177 of PBP.  (Severity: 4)
    

    这是一种自动发现其他人所说内容的方法:print 是一个内置函数。

    【讨论】:

      【解决方案2】:

      print 是一个内置函数;要调用名为该的子例程,请使用 &print(...) 而不是 print(...)

      【讨论】:

      • 这对&if(...) 有效吗?令我惊恐的是——是的! 即使使用 strict sub if { say 'If!'; } 也可以! Perl 是“新”PL/I!!
      • &if 只是一个保存函数类型的变量。你害怕你可以使用$if@if%if吗?
      • *{""} = sub {print "empty string"}; &{""}()
      • @mob,对&if 并不感到害怕——因为我不会在$if 或类似的地方——但我可以用sub if {...} 创建它。但正如 ysth 所示,*{''} 可以是一个子
      【解决方案3】:

      我认为您的问题是您正在调用 print 到您的子例程,并且 print 已经在 perl 中定义。

      尝试更改子程序的名称,例如,这对我有用:

      my %file_attachments = (
               'test1.zip'  => { 'price' => '10.00', 'desc' => 'the 1st test'},
               'test2.zip'  => { 'price' => '12.00', 'desc' => 'the 2nd test'},
               'test3.zip'  => { 'price' => '13.00', 'desc' => 'the 3rd test'},
               'test4.zip'  => { 'price' => '14.00', 'desc' => 'the 4th test'}
                         );
      
                         my $a="test5.zip";
                         my $b="the 5th test";
      
               $file_attachments{$a}->{'price'} = '18.00';
               $file_attachments{$a}->{'desc'} =$b;
      
      
              printtest(%file_attachments);
      
      
      sub printtest{
      
      my %file =@_;
      
      foreach my $line (keys %file) {
              print "$line: \n";
               foreach my $elem (keys %{$file{$line}}) {
                print "  $elem: " . $file{$line}->{$elem} . "\n";
          }
                       }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 2014-10-18
        • 2013-05-11
        • 1970-01-01
        • 1970-01-01
        • 2011-08-18
        相关资源
        最近更新 更多