gpt4 book ai didi

arrays - 作业中的 Perl 'context'

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

作为 Perl 的新手,我只是不明白以下结果背后隐藏着什么样的哲学:

$cnt1 = @arr;           
# this gives the length of the array 'arr'
$cnt2 = @arr[ @indices_arr ];
# this gives the last element of the @arr[ @indices_arr ];

有谁能解释一下两者的区别吗?

最佳答案

来自perldoc perldata :

Assignment to a scalar evaluates the right-hand side in scalar context...

还有:

If you evaluate an array in scalar context, it returns the length of the array.

还有:

Slices in scalar context return the last item of the slice.

@arr 是一个数组; @arr[ @indices_arr ] 是一个数组切片。

<小时/>

至于其背后的哲学:列表和数组在 Perl 中是不同的数据类型,具有不同的行为(不要被切片中使用的 @ 符号所抛出,切片是列表,而不是数组)。请参阅Arrays vs. Lists in Perl: What's the Difference?深入解释两者之间的差异。

关于arrays - 作业中的 Perl 'context',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047928/

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