Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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/11.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/2/shell/5.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 如何在| |运算中将变量中的0用作字符串而不是布尔值?_Arrays_Perl_Boolean - Fatal编程技术网

Arrays 如何在| |运算中将变量中的0用作字符串而不是布尔值?

Arrays 如何在| |运算中将变量中的0用作字符串而不是布尔值?,arrays,perl,boolean,Arrays,Perl,Boolean,我有两个变量的比较操作来选择一个有值的变量(包括0)。脚本必须找到两个变量中哪个有值,哪个没有值 以下是一个例子: my @ara = (0,1,,3,4,5); my @arb = (1,,3,4,,); my $output; my $i = 0; for (0..@ara) { $output .= $ara[$i] || $arb[$i]; # a non-empty value has to be added to the $out

我有两个变量的比较操作来选择一个有值的变量(包括0)。脚本必须找到两个变量中哪个有值,哪个没有值

以下是一个例子:

    my @ara = (0,1,,3,4,5);
    my @arb = (1,,3,4,,);
    my $output;
    my $i = 0;
    for (0..@ara) {
       $output .= $ara[$i] || $arb[$i]; # a non-empty value has to be added to the $output.
       $i++;
    }

print $output;
输出必须为:
013345

但是,Perl将
@ara
(即
0
)中的第一个元素视为Nill(nothing)。并且认为元素根本没有值。我的实际输出是:
113345

问题是:如何“即时”将
0
s从布尔值转换为字符串

我知道,我可以将我的
@ara
呈现如下:

@ara = ('0', '1', '2'..);
但我不能这样做,因为整个剧本中有一些条件。 我唯一能做的就是将确切的单独元素转换成所需的格式(比如:
doSomething($ara[$I])
)。 我也试过:
$ara[$I]。”但它不起作用


谢谢

有两种方法可以做到这一点:

defined
如果定义了变量,则返回true

或者是
/
操作符,从perl 5.10开始,除了
0
'
之外,它允许您执行与
|
非常相似的操作,而
'
通常被视为“false”

但是您不必做任何事情来“转换”,因为perl会自动进行转换——如果您在做字符串,它会将
0
视为字符串,如果您在做许多事情,它会将
“0”
视为数值

您可以:

 $output .= $ara[$i] // $arb[$i];
或:

您必须在较旧版本的perl上使用后者,而
/
不可用

不过也要注意——在for循环中有一个fencepost错误。您可能应该使用:

for (0..$#ara) {
因为标量上下文中的
@ara
返回元素数(
5
)-但是您的标记是
0
<代码>4

您可能还应该注意到:

my @ara = (0,1,,3,4,5);
不会像你想象的那样-双逗号不是空值,只是被忽略了

use Data::Dumper;
print Dumper \@ara;
给出:

$VAR1 = [
          0,
          1,
          3,
          4,
          5
        ];
也许你的意思更像:

#!/usr/bin/env perl
use strict;
use warnings;

my @ara = ( 0, 1, undef, 3, 4, 5 );
my @arb = ( 1, undef, 3, 4, undef, );

my $output;
for my $index ( 0 .. $#ara ) {
    # a non-empty value has to be added to the $output.
    $output .= $ara[$index] // $arb[$index];  
}

print $output;

有两种方法可以做到这一点:

defined
如果定义了变量,则返回true

或者是
/
操作符,从perl 5.10开始,除了
0
'
之外,它允许您执行与
|
非常相似的操作,而
'
通常被视为“false”

但是您不必做任何事情来“转换”,因为perl会自动进行转换——如果您在做字符串,它会将
0
视为字符串,如果您在做许多事情,它会将
“0”
视为数值

您可以:

 $output .= $ara[$i] // $arb[$i];
或:

您必须在较旧版本的perl上使用后者,而
/
不可用

不过也要注意——在for循环中有一个fencepost错误。您可能应该使用:

for (0..$#ara) {
因为标量上下文中的
@ara
返回元素数(
5
)-但是您的标记是
0
<代码>4

您可能还应该注意到:

my @ara = (0,1,,3,4,5);
不会像你想象的那样-双逗号不是空值,只是被忽略了

use Data::Dumper;
print Dumper \@ara;
给出:

$VAR1 = [
          0,
          1,
          3,
          4,
          5
        ];
也许你的意思更像:

#!/usr/bin/env perl
use strict;
use warnings;

my @ara = ( 0, 1, undef, 3, 4, 5 );
my @arb = ( 1, undef, 3, 4, undef, );

my $output;
for my $index ( 0 .. $#ara ) {
    # a non-empty value has to be added to the $output.
    $output .= $ara[$index] // $arb[$index];  
}

print $output;

对于Perl 5.10或更高版本,可以使用运算符
/
而不是
|

$output .= $ara[$i] // $arb[$i];
对于5.10下的Perl,可以使用
?:

$output .= defined $ara[$i] ? $ara[$i] : $arb[$i];
我还要指出你例子中的一些错误:

  • 逗号折叠在数组定义中,使用
    unde
    添加 未定义的值
  • 标量上下文中的
    @ara
    返回数组中元素的计数, 不是最后一个索引。因此的
    循环超出范围
    @ara
  • 您可以在
    for
    循环中分配
    $i
  • 所以你的例子可能是这样的:

    my @ara = (0,1,undef,3,4,5);
    my @arb = (1,undef,3,4,undef,undef);
    my $output;
    for my $i (0..$#ara) {
        $output .= $ara[$i] // $arb[$i];
    }
    print $output;
    

    对于Perl 5.10或更高版本,可以使用运算符
    /
    而不是
    |

    $output .= $ara[$i] // $arb[$i];
    
    对于5.10下的Perl,可以使用
    ?:

    $output .= defined $ara[$i] ? $ara[$i] : $arb[$i];
    
    我还要指出你例子中的一些错误:

  • 逗号折叠在数组定义中,使用
    unde
    添加 未定义的值
  • 标量上下文中的
    @ara
    返回数组中元素的计数, 不是最后一个索引。因此
    循环超出范围
    @ara
  • 您可以在
    for
    循环中分配
    $i
  • 所以你的例子可能是这样的:

    my @ara = (0,1,undef,3,4,5);
    my @arb = (1,undef,3,4,undef,undef);
    my $output;
    for my $i (0..$#ara) {
        $output .= $ara[$i] // $arb[$i];
    }
    print $output;
    

    看起来您需要定义的or运算符,它从Perl的v5.10开始就可用

    如果
    $x
    为真,则
    $x
    的计算结果为
    $x
    ,否则
    $y

    同样,如果定义了
    $x
    ,则
    $x/$y
    的计算结果为
    $x
    ,否则
    $y

    此语句将修复您的程序

    $output .= $ara[$i] // $arb[$i]
    
    但你也应该注意人们提出的其他问题。特别是,你必须始终


    在您编写的每个Perl程序的开头。这将拾取相对简单的错误,否则您可能会忽略这些错误。看起来您需要定义的or运算符,它从Perl的v5.10开始就可用

    如果
    $x
    为真,则
    $x
    的计算结果为
    $x
    ,否则
    $y

    同样,如果定义了
    $x
    ,则
    $x/$y
    的计算结果为
    $x
    ,否则
    $y

    此语句将修复您的程序

    $output .= $ara[$i] // $arb[$i]
    
    但你也应该注意人们提出的其他问题。特别是,你必须始终


    在您编写的每个Perl程序的开头。这将拾取相对简单的错误,否则您可能会忽略这些错误。=$ara[$i]/$arb[$i]
    @Borodin,谢谢!上帝保佑你!!!!它起作用了!请你把答案写出来,我会投赞成票的!谢谢一行中有两个逗号不会创建空值。你应该