为什么我的多级哈希按我期望的方式打印?
这是代码,它不起作用,我想做的是将哈希值传递给子例程(又名函数),但它给出了一些奇怪的输出。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
perlcritic 可以成为调试 Perl 代码的便捷工具:
这是一种自动发现其他人拥有的内容的方法声明:
print
是一个内置函数。perlcritic can be a handy tool in debugging Perl code:
This is an automated way of discovering what others have stated: that
print
is a built-in function.print
是一个内置函数;要调用名为该子例程,请使用&print(...)
而不是print(...)
。print
is a builtin function; to call a subroutine named that, use&print(...)
instead ofprint(...)
.我认为你的问题是你正在调用
print
到你的子例程,并且 print 已经在 perl 中定义了。尝试更改子程序的名称,例如,这对我有用:
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: