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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
如何仅从perl数组中grep定义的值_Perl - Fatal编程技术网

如何仅从perl数组中grep定义的值

如何仅从perl数组中grep定义的值,perl,Perl,我有很多数组,它们有一些已定义的数组元素,然后是一些未定义的元素 在以下简单情况下不会出现此问题: my @x = sort {$a <=> $b} grep {defined} @{ $args{data} }; sub boxplot_series { my %args = ( output_type => 'eps', @_ # defaults ); my @labels = sort keys %{ $args{data} }

我有很多数组,它们有一些已定义的数组元素,然后是一些未定义的元素

在以下简单情况下不会出现此问题:

my @x = sort {$a <=> $b} grep {defined} @{ $args{data} };
sub boxplot_series {
    my %args = (
        output_type => 'eps', @_ # defaults
    );
    my @labels = sort keys %{ $args{data} };# $args{data} should be a hash of arrays
    my ($data_fh, $data_filename) = tempfile(UNLINK => 1);
    my $x = 1;
    foreach my $set (@labels) {
        my $n = scalar @{ $args{data}{$set} };
        if ($n == 0) {
           confess "$set has no values.\n";
        }
        my @x = sort {$a <=> $b} grep {defined $_} @{ $args{data}{$set} };
        if (grep undef @x) {
            confess "\@x has undef.";
        }
        if (scalar @x == 0) { next }
        my $n4 = floor(($n+3)/2)/2;
        my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);#d <- c(1, n4, (n + 1)/2, n + 1 - n4, n)
        my @sum_array = $x;
        $x++;
        foreach my $e (0..4) {
           my $floor = floor($d[$e]-1);
           my $ceil  =  ceil($d[$e]-1);
# undef values can get through here too
           push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
        }
        ....
my@x=sort{$a$b}grep{defined}{$args{data};
但这允许未定义的值在以下复杂情况下通过:

my @x = sort {$a <=> $b} grep {defined} @{ $args{data} };
sub boxplot_series {
    my %args = (
        output_type => 'eps', @_ # defaults
    );
    my @labels = sort keys %{ $args{data} };# $args{data} should be a hash of arrays
    my ($data_fh, $data_filename) = tempfile(UNLINK => 1);
    my $x = 1;
    foreach my $set (@labels) {
        my $n = scalar @{ $args{data}{$set} };
        if ($n == 0) {
           confess "$set has no values.\n";
        }
        my @x = sort {$a <=> $b} grep {defined $_} @{ $args{data}{$set} };
        if (grep undef @x) {
            confess "\@x has undef.";
        }
        if (scalar @x == 0) { next }
        my $n4 = floor(($n+3)/2)/2;
        my @d = (1, $n4, ($n +1)/2, $n+1-$n4, $n);#d <- c(1, n4, (n + 1)/2, n + 1 - n4, n)
        my @sum_array = $x;
        $x++;
        foreach my $e (0..4) {
           my $floor = floor($d[$e]-1);
           my $ceil  =  ceil($d[$e]-1);
# undef values can get through here too
           push @sum_array, (0.5 * ($x[$floor] + $x[$ceil]));
        }
        ....
sub-boxplot\u系列{
我的%args=(
输出类型=>eps',@#默认值
);
my@labels=sort keys%{$args{data};#$args{data}应该是数组的散列
my($data\u fh,$data\u filename)=临时文件(UNLINK=>1);
我的$x=1;
foreach my$set(@labels){
my$n=scalar@{$args{data}{$set};
如果($n==0){
承认“$set没有值。\n”;
}
my@x=sort{$a$b}grep{defined$}@{$args{data}{$set};
如果(grep undf@x){
承认“\@x有未定义项。”;
}
如果(标量@x==0){next}
my$n4=楼层($n+3)/2)/2;
我的@d=(1,$n4,($n+1)/2,$n+1-$n4,$n);#d它工作正常

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my %args = (data => [4, 2, undef, 3, undef, 1]);

my @x = sort {$a <=> $b} grep {defined} @{ $args{data} };

say for @x;
之前,在同一范围内排序
。这是因为
$a
(和
$b
)是特殊的全局变量,您不应该重新声明它们。现在您知道原因了

有了它,你也可以

"my $a" used in sort comparison at ...

您的代码对我有效,在
@x
中没有未定义的值。您一定是做错了什么。请编辑问题以显示显示显示此行为的整个程序。此外,请在不使用
排序{$a$b}的情况下尝试该问题
将代码错误的代码量降至最低。很抱歉浪费大家的时间。错误在代码的其他地方,但我现在无法关闭或删除我的问题。我投票关闭此问题,因为问题
if(grep unde@x)中有错误
?你是说
grep!defined,@x
$a
会影响排序,但OP说“这允许未定义的值通过”。@AndyLester:Hm,但它会在数值比较中使用未初始化的值$a()
警告可能会让人认为比较中存在未初始化的值。噢,可能是这样。