gpt4 book ai didi

string - 在 perl 中获取字符串的 hahes 键

转载 作者:行者123 更新时间:2023-12-03 23:33:22 27 4
gpt4 key购买 nike

我不明白为什么 $test 是 '1' 而不是 'foo'。有什么方法可以将 'foo'(以 %hash 写成)写入 $test 吗?

#/usr/bin/perl
use warnings;
use strict;
use utf8;

my %hash;
$hash{'test'} = {'foo' => 1};

print keys %{$hash{'test'}}; #foo
print "\n";
print values %{$hash{'test'}}; #1
print "\n";
print %{$hash{'test'}}; #foo1
print "\n";

# here is the part I do not understand
my $test = keys %{$hash{'test'}};
print "$test\n"; #1 but why? I was expecting #foo

如何在 $test 中推送“foo”?

最佳答案

keys() 返回一个列表。但是您对标量的分配 (my $test = keys ...) 会将其置于标量上下文中。因此,它被评估为列表的长度,在您的情况下为 1

关于string - 在 perl 中获取字符串的 hahes 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66422719/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com