为什么我的多级哈希按我期望的方式打印?

发布于 2024-11-05 05:33:29 字数 1075 浏览 0 评论 0原文

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

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)   

Here's the code and its not working, What I am trying to do is to pass Hash of Hashes to subroutine aka function, but it gives some odd output.

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";
    }
                 }

OUTPUT::::

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-11-12 05:33:29

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 是一个内置函数。

perlcritic can be a handy tool in debugging Perl code:

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)

This is an automated way of discovering what others have stated: that print is a built-in function.

树深时见影 2024-11-12 05:33:29

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

print is a builtin function; to call a subroutine named that, use &print(...) instead of print(...).

瞄了个咪的 2024-11-12 05:33:29

我认为你的问题是你正在调用 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";
    }
                 }
}

I think your problem is that you are calling print to your subroutine, and print is already defined in perl.

Try changing the name of the subrutine, for instance, this works for me:

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";
    }
                 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文