Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays perl中的散列变量和数组给出了一个笨拙的输出_Arrays_Perl - Fatal编程技术网

Arrays perl中的散列变量和数组给出了一个笨拙的输出

Arrays perl中的散列变量和数组给出了一个笨拙的输出,arrays,perl,Arrays,Perl,我刚刚开始编写Perl脚本,同时创建一个数组并从哈希变量获取它。我得到了一个笨拙的输出 代码如下: %name= ( "xyz",1 ,"is",2, "my",3,"name",4); %copy=%name; $size=%name; print " your rank is : $copy{'xyz'}"; print " \n"; print " the size of the array is : $size"; 产出如下: your rank is : 1 the size o

我刚刚开始编写Perl脚本,同时创建一个数组并从哈希变量获取它。我得到了一个笨拙的输出

代码如下:

%name= ( "xyz",1 ,"is",2, "my",3,"name",4);
%copy=%name;
$size=%name;
print " your rank is : $copy{'xyz'}";
print " \n";
print " the size of the array is : $size";
产出如下:

your rank is : 1 
 the size of the array is : 3/8

为什么数组的大小是3/8?

这是有关哈希的内部信息,请检查:

如果在标量上下文中计算哈希,则如果哈希为空,则返回false。如果存在任何键/值对,则返回true;更准确地说,返回的值是一个字符串,由使用的桶数和分配的桶数组成,用斜杠分隔。这非常有用,仅用于确定Perl的内部哈希算法在数据集上的性能是否很差

因此,这里具体地说,这意味着您在散列中分配了8个bucket,并且使用了其中的三个

要获得尺寸,请使用:

$size = keys %hash; # scalar is implicit here
print(scalar keys %hash);

如果要使用
标量
查找键/值的数量:

my %name= ( "xyz",1 ,"is",2, "my",3,"name",4);

my %copy = %name;

my $size = scalar keys %name;

print "your rank is : $copy{'xyz'}\n";
print "the size of the array is : $size\n";

代码中没有数组。你认为哪个变量是数组?