#perl #hash #reference #key
#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";
Есть ли способ разыменования%hash в приведенном выше примере без использования временной переменной $temp_ref ?
Ответ №1:
Вам нужны фигурные скобки, а не круглые скобки:
print join(" ",keys %{ $other_hash{"key1"} }), "n";
# ---^-- here --^-- and here
Комментарии:
1. Спасибо, я знал, что это должна быть какая-то глупая маленькая деталь вроде этой 🙂