gpt4 book ai didi

arrays - 如果 fetchrow_array 没有返回行,如何将数组分配给默认值

转载 作者:行者123 更新时间:2023-12-02 09:24:45 25 4
gpt4 key购买 nike

我想我可以把它分成两个问题。

1.如果我想一步完成以下某件事该怎么办?

if($sth->fetchrow_array is empty /undef){
@parent = @default;
}

我尝试过类似的方法,但失败了。

@parent = $sth->fetchrow_array or @default;
## Problem is it assign undef if array1 is undef.

@parent = $sth->fetchrow_array || @default;
## problem is it assign scalar value to the parent if it's not undef

2.下面是示例代码。为什么我在第二个输出中得到标量值?

@default = (2,3,4);

## First output
@parent = $sth->fetchrow_array or @default;
print @parent;

##Second output;
@parent = $sth->fetchrow_array || @default;
print @parent;

最佳答案

我建议您使用fetchrow_arrayref,而不是fetchrow_array,如果没有更多数据要返回,它会返回undef

然后你就可以写了

@parent = @{ $sth->fetchrow_arrayref || \@default };

或者,如果方便的话,您可以将结果保留为引用

$parent = $sth->fetchrow_arrayref || \@default;

关于arrays - 如果 fetchrow_array 没有返回行,如何将数组分配给默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793335/

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