【问题标题】:How do you directly dereference a hash reference stored in another hash?如何直接取消引用存储在另一个哈希中的哈希引用?
【发布时间】:2021-02-03 19:50:27
【问题描述】:

如果我将一个哈希的引用存储在另一个哈希中,是否可以在不使用临时变量的情况下直接取消引用它?

$myhash{"color"}="blue";
$myhash{"weight"}=12;

# Store a reference to %myhash in $other_hash{"key1"}

$other_hash{"key1"}=\%myhash;

# Use that reference with the help of a temporary variable $temp_ref - it works

$temp_ref=$other_hash{"key1"};
print join(" ",keys %$temp_ref),"\n";

# Try using that reference without a temporary variable -
# this produces an error "Scalar found where operator expected". Why?

print join(" ",keys %($other_hash{"key1"})),"\n";

有没有办法在不使用临时变量 $temp_ref 的情况下取消引用上面示例中的 %hash?

【问题讨论】:

    标签: perl hash reference key


    【解决方案1】:

    你需要大括号而不是圆括号:

    print join(" ",keys %{ $other_hash{"key1"} }), "\n";
    #                 ---^-- here            --^-- and here
    

    【讨论】:

    • 谢谢,我知道这一定是一些愚蠢的小细节 :)
    猜你喜欢
    • 2014-03-30
    • 2014-05-08
    • 1970-01-01
    • 2011-10-11
    • 2012-05-28
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2014-01-30
    相关资源
    最近更新 更多