前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Perl Learning - 3 (A

Perl Learning - 3 (A

作者头像
py3study
发布2020-01-10 11:39:19
发布2020-01-10 11:39:19
1.1K0
举报
文章被收录于专栏:python3python3

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);

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档