加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

数组 – 赋值中的Perl“上下文”

发布时间:2020-12-16 06:21:04 所属栏目:大数据 来源:网络整理
导读:作为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 ]; 有人能解释两者之间的区别吗? 解决方法
作为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中是不同的数据类型,具有不同的行为(不要被切片中使用的@sigil抛出,切片是列表,而不是数组).有关两者之间差异的深入解释,请参见Arrays vs. Lists in Perl: What’s the Difference? .

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读