gpt4 book ai didi

perl - 在 Perl 中打印哈希值

转载 作者:行者123 更新时间:2023-12-02 13:30:55 25 4
gpt4 key购买 nike

我想知道下面的代码为什么打印“2/8”。

#!/usr/bin/perl
#use strict;
#use warnings;
%a = ('a'=>'dfsd','b'=>'fdsfds');
print %a."\n";

最佳答案

您正在 scalar context 中打印哈希值通过将其与字符串 '\n' 连接起来

If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash.

2/8 表示在分配的 8 个存储桶中,2 已被触及。考虑到您插入了 2 个值,到目前为止效果良好:)

除了评估哈希函数的性能外,该值显然没有任何用处。使用 print %a; 打印其内容。

关于perl - 在 Perl 中打印哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15563826/

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