Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 由<;生成的数组的大小>;?_Arrays_Perl_Size_Glob - Fatal编程技术网

Arrays 由<;生成的数组的大小>;?

Arrays 由<;生成的数组的大小>;?,arrays,perl,size,glob,Arrays,Perl,Size,Glob,如果我想找到由生成的数组的大小,为什么下面的方法不起作用 print "Number of .DOC and .PDF files is: " scalar(<*.PDF *.DOC>); print”的.DOC和.PDF文件数为:“scalar(); 它给出了第一个匹配文件的名称。让我们先澄清一些误解 [1] 从不返回数组。运算符不返回数组[2];它们返回标量。有时没有,有时一个,有时很多 scalar影响运算符,而不是它们返回的值。具体地说,scalar导致在标量上下文中计

如果我想找到由
生成的数组的大小,为什么下面的方法不起作用

print "Number of .DOC and .PDF files is: " scalar(<*.PDF *.DOC>);
print”的.DOC和.PDF文件数为:“scalar();

它给出了第一个匹配文件的名称。

让我们先澄清一些误解

  • [1] 从不返回数组。运算符不返回数组[2];它们返回标量。有时没有,有时一个,有时很多

  • scalar
    影响运算符,而不是它们返回的值。具体地说,
    scalar
    导致在标量上下文中计算中的运算符

  • 上下文如何影响操作符取决于每个操作符。唯一不变的是,它们必须在标量上下文中只返回一个标量。在标量上下文中,他们选择返回的内容中有很多内容

在标量上下文中,
glob
充当迭代器,返回它将在列表上下文中返回的下一项,或者
unde
表示没有其他要返回的值


解决方案:

  • 高效内存解决方案:

    my $num_files = 0;
    ++$num_files while <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: $num_files\n";
    
    my @qfns = <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: " . @qfns . "\n";
    
    print "Number of .DOC and .PDF files is: " . ( () = <*.PDF *.DOC> ) . "\n";
    
    my$num\u files=0;
    ++$num_文件,而;
    打印“.DOC和.PDF文件的数量为:$num\u文件\n”;
    
  • 更快的解决方案:

    my $num_files = 0;
    ++$num_files while <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: $num_files\n";
    
    my @qfns = <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: " . @qfns . "\n";
    
    print "Number of .DOC and .PDF files is: " . ( () = <*.PDF *.DOC> ) . "\n";
    
    my@qfns=;
    打印“.DOC和.PDF文件的数量为:”@qfns。“\n”;
    
  • 上述内容的内联版本:

    print "Number of .DOC and .PDF files is: " . @{[ <*.PDF *.DOC> ]} . "\n";
    
    print“.DOC和.PDF文件的数量为:”@{[  ]} . “\n”;
    
  • 更快、更少浪费但更高级的解决方案:

    my $num_files = 0;
    ++$num_files while <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: $num_files\n";
    
    my @qfns = <*.PDF *.DOC>; 
    print "Number of .DOC and .PDF files is: " . @qfns . "\n";
    
    print "Number of .DOC and .PDF files is: " . ( () = <*.PDF *.DOC> ) . "\n";
    
    print“.DOC和.PDF文件的数量为:”。( () =  ) . “\n”;
    
    in scalar上下文返回其RHS返回的标量数

  • 清洁剂/清洁剂

    my $num_files = () = <*.PDF *.DOC>;
    print "Number of .DOC and .PDF files is: $num_files\n";
    
    my$num_files=()=;
    打印“.DOC和.PDF文件的数量为:$num\u文件\n”;
    

  • glob(qq)

  • 除了在某些情况下的
    @a
    (包括
    \@a
    @a=LIST
    ,以及像
    推@a,LIST
    这样的数组操纵器)


  • @user2864740如何在同一语句中将列表转换为数组?哦,我错了:请参见“在标量上下文中,glob通过这样的文件名扩展进行迭代…”我将使用串联而不是
    0+
    来强制执行标量上下文-例如,
    print”的.DOC和.PDF文件数为:“。( () =  );@Dave Cross,我也会这么做,既然我不坚持OP使用的东西,我已经更新了我的答案