perl - 如何直接取消引用存储在另一个哈希中的哈希引用?
问题描述
如果我将一个哈希的引用存储在另一个哈希中,是否可以在不使用临时变量的情况下直接取消引用它?
$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?
解决方案
您需要大括号而不是括号:
print join(" ",keys %{ $other_hash{"key1"} }), "\n";
# ---^-- here --^-- and here
推荐阅读
- html - 我的网站最初加载时如何停止转换?
- python - 如何从 python 中的 Spark 数据框中访问特定列?
- json - 在 Swift 中将解码的 JSON 数据设置为 UITextFields - 非字符串的问题
- php - 可恢复的致命错误:无法将类 Produk 的对象转换为第 12 行 C:\xampp\htdocs\oophp\produk.php 中的字符串
- reactjs - 使用 React Native 和 Expo 创建应用内 PDF 查看器的可能方法是什么?
- unreal-engine4 - UE4 - 使用不同武器时更换 ADS 相机
- python - 如何检查消息是否包含频道提及(Discord.py)
- php - 如何通过保持 WordPress 命名约定来处理 composer 的自动加载?
- android - adb + android:在 Linux 终端中以编程方式访问应用程序生成的剪贴板数据
- typescript - 无法使用 TypeORM + pgAdmin4 连接到在 docker 上运行的本地 PostgreSQL 数据库