文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Perl Learning - 3 (A

2023-01-31 02:07

关注
List & Array
 
While scalar is single value, list is a list of scalars in order. Every element of a list is a dependant scalar, it can be number or characters.
Array is the variable of list, list is the values of array. In Perl array and list are almost the same meaning: a list of scarlars.
Every element of array is kept with its index, starting from [0].
 
$fred[0]="yabba";
$fred[1]="dabba";
$fred[2]="doo";
 
Arrays and Scalars have different namespaces, such as $fred[0] and $fred can be used at the same time, Perl won't be confused, but maybe the maintainer will, so don't play Perl like that.
We can do whatever to array elements like we do to a scalar.
 
print $fred[0];
$fred[2]="didley";
print $fred[$number-1];
 
Like the last example, we can use variable and expression in array's index. Element without a value will be undef.
Array can grow its length automatically if you give values to a certain index.
 
$rocks[0]='bedrock';
$rocks[1]='slate';
$rocks[2]='lava';
$rocks[3]='crushed rock';
$rocks[99]='schist';      # 95 undef elements created!
 
There are two ways to directly get the last element of an array:
 
$rocks[$#rocks]='hard rock';
$rocks[-1]='hard rock';
$#rocks is the index of last element, both above ways are correct, but [-1] is more popular ^_^
 
list/array can be writen in ( ), split by ',' between neighbour elements.
 
(1, 2, 3)
(1, 2, 3,) # same as above
()  # empty list, containing 0 element
(1 .. 100) # a list of 100 int
(5 .. 1) # empty list, .. is order sensitive
(2, 2 .. 6, 10, 12 ) # same as (0, 2, 3, 4, 5, 6, 10, 12)
("fred", "barney", "betty", "milma", "dino") # list of characters
($m .. $n)
(0 .. $#rocks)
($m, 17) # two values
($m+$0, $p+$q) # two values
 
The last 4 examples shows element can be variable and/or expression too.
 
List of characters are very common, qw was designed for this. qw means 'quotes words', it works as a piar of ' .
 
qw(fred barney betty wilma dino) # same as ('fred', 'barney', 'betty', 'milma', 'dino')
qw will ignore the whitespace (spaces, tabs newlines), and the ( ) can be other symbols.
qw ! fred barney betty wilma dino !
qw# fred barney betty wilma dino # # like comments
qw( fred barney betty wilma dino )
qw{ fred barney betty wilma dino }
qw[ fred barney betty wilma dino ]
qw< fred barney betty wilma dino >
 
qw{
/usr/dict/words
/home/rootbeer/.ispell_english # a good way representing Unix paths
}
 
Like scalar, we can give values to list.
 
($fred, $barney, $dino)=("flintstone", "rubble", undef);
($fred,$barney)=($barney,$fred); # a simple way to replace values of varables
($betty[0],$betty[1])=($betty[1],$betty[0]);
($fred,$barney) = qw<flintstone rubble slate granite>; # slate and granite will be ignored
($wilma,$dino) = qw[flintstone];   # $dino is undef
 
We can use @ to give values to an array.
 
@rocks=qw/bedrock slate lava/;
@tiny=();   # empty list
@stuff=(@giant, undef, @giant);
 
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯