微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 为什么reverse()不会改变我的数组?

当我使用reverse()或sort()时,如果我想稍后使用它,我总是需要将return语句保存到变量中.
@array=qw(Nick Susan Chet Dolly Bill);
@array = reverse(@array);

为什么这与使用push(),pop()或shift()不同,您可以只调用函数并更改数组?

@array=qw(Nick Susan Chet Dolly Bill);
push(@array,"Bruce");

那么这些“功能”究竟有什么区别呢?

解决方法

perldoc perlfunc提供了一个主要线索:

Functions for real @ARRAYs

07001,
07002,
07003,
07004,
07005,
07006,
07007,
07008

Functions for list data

07009,
070010,
070011,
070012,
070013,
070014,
070015

并且perldoc perlfaq4解释了数组和列表之间的区别(强调我自己):

What is the difference between a list and an array?

(contributed by brian d foy)

A list is a fixed collection of scalars. An array is a variable that
holds a variable collection of scalars. An array can supply its
collection for list operations,so list operations also work on arrays

Array operations,which change the scalars,rearrange them,or
add or subtract some scalars,only work on arrays. These can’t work on a list,which is fixed. Array operations include
shift,unshift,push,pop,and splice.

简而言之,像reverse这样的列表操作是为列表设计的,无法修改.

他们可以接受数组的事实仅仅是列表支持的副作用.

原文地址:https://www.jb51.cc/Perl/171995.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐