【问题标题】:dereferencing hash from params从参数中取消引用哈希
【发布时间】:2011-02-27 09:18:21
【问题描述】:

此代码有效:

  my $href = shift @_;    # get reference to hash
  my %h = %{$href};       # dereference hash

这个没有:

  my %h = %{shift @_};

还有这个:

  my %h = ${$_[0]}

为什么?

===============================

确切地说还有一次:

 1 #!/usr/bin/perl -w
  2 use strict;
  3 use warnings;
  4 
  5 my %h;
  6 sub a {
  7     
  8     # that works - result 1
  9     my $href = $_[0] || shift;
 10     %h = %{$href};
 11     
 12     # that does not work - result 0
 13     # my %h = %{$_[0]};
 14     
 15     # as well as that one - result 0
 16     # my %h = %{shift @_};
 17     $h{1}=2;
 18 }
 19 
 20 a(\%h);
 21 print scalar (keys %h) . "\n";

换句话说,第 16 行 - 它没有。

【问题讨论】:

  • 我更新了我的答案,你的问题完全不相关。

标签: perl hash dereference


【解决方案1】:

这会起作用。

  my %h = %{shift @_};

这不会。

  my %h = ${$_[0]} # not ${$_[0]}

那个印记应该是%

  my %h = %{$_[0]}

还有use warnings;use strict;


提示:您上面的示例不起作用的原因只是一个示例没有声明词法变量%h = %{$href}; 不是my %h = %{$href};

【讨论】:

  • 没有人使用警告和严格吗?
  • @demosthenex:是的,不幸的是很多人。
猜你喜欢
  • 2012-05-28
  • 1970-01-01
  • 2011-10-11
  • 2016-04-25
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
相关资源
最近更新 更多