Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl 语言.

Similar presentations


Presentation on theme: "Perl 语言."— Presentation transcript:

1 Perl 语言

2 第三章 列表与数组 1 列表直接量 2 列表相关操作符 3 foreach循环结构 4 标量上下文与列表上下文

3 3.1 列表直接量 列表是指数据,而数组是其变量名; 每个数组标量都有一个列表,列表可以为空。 标量 标量变量 列表(标量集合) 数组

4 列表示例 包含5个元素的列表 35 12.4 “hello” 1.72e30 “bye\n” 1 2 3 4

5 列表特点 35 12.4 “hello” 1.72e30 “bye\n” 1 2 3 4 1.每个元素都是单独的标量变量 2.元素值是有序的
1 2 3 4 列表特点 1.每个元素都是单独的标量变量 2.元素值是有序的 3.元素索引从‘0’开始,每次递增1 4.元素值之间互不相关, “数字”,“字符串”,“undef”或其他类型数据 5.数组/列表的“无限制性”

6 访问数组中的元素 数组中的元素由连续整数编号,其从0开始,每增加一个元素,其索引值加一,如
$fred[0] = “yabba”; $fred[1]= “dabba”; $fred[2] = “doo” 数组名字(fred)和标量属于完全不同的命名空间。 同一程序可以同时包含$fred(标量变量)和fred命名的数组(运行例子)。

7 访问数组中的元素 数组元素($fred[2])使用与标量变量($fred)相同 print $fred[0] ;
$fred[2] = “diddley”; $fred[1] .= “whatsis”; $number = ; print $fred[$number-1] ; #与print $fred[1]一样 $blank = $fred [142_857]; #该数组元素未存值,undef $blank = $mel; #$mel 未初始化,undef

8 访问数组中的元素 特殊数组索引 负数数组索引:从数组的尾端往回计数。 超出数组大小的负数索引值,返回undef,不会绕回数组尾部(运行例子)
$number = ; print $fred[$number-1] ; #和print $fred[1]一样 print $fred[$number-2],"\n" ; print $fred[$number-3],"\n" ; print $fred[$number-4],"\n" ; print $fred[$number-5],"\n" ; print $fred[$number-6],"\n" ; print $fred[$number-7],"\n" ; 负数数组索引:从数组的尾端往回计数。 超出数组大小的负数索引值,返回undef,不会绕回数组尾部(运行例子)

9 访问数组中的元素 特殊数组索引 $rocks[0] = ‘bedrock’; #一个元素 $rocks[1] = ‘slate’; #又一个
$rocks[2] = ‘lava’; #又一个 $rocks[3] = ‘crushed rock’; #又一个 $rocks[99] = ‘schist’; #现在有95 个undef 元素 $end = $#rocks; #99, $number_of_rocks = $end + 1; #正确,更好的方法 $rocks[$#rocks] = ‘hard rock’; #the last rock $rocks[-1] = ‘hard rock’; #better $dead_rock = $rocks[-100]; #‘bedrock’ $rocks[-200] = ‘crystal’; #严重错误(fatal error!)

10 列表直接量 列表直接量:圆括号内用逗号隔开的一串数据 范围操作符:.. (1,2,3) #含有1,2,3 的列表
(1,2,3) #含有1,2,3 的列表 (1,2,3,) #同上,最后一个逗号被忽略 () #空列表-0 个元素 ( ) #包含100 个整数的列表 范围操作符:.. (1 ..5) #同(1,2,3,4,5) ( ) #同上— 转换成整数 (5 ..1) #空— ..左值应小于右值,否则为空 (0,2.. 6,10,12) #同(0,2,3,4,5,6,10,12) ($m ..$n) #由$m 和$n 的值决定 (0 .. $#rocks) (“fred”, “barney”, “betty”, “wilma”, “dino”)

11 qw 简写 Perl中经常需要建立单词列表,可以使用qw简写。
qw-“quoted words”或“quoted by whitespace,” (“fred”, “barney”, “betty”, “wilma”, “dino”) qw(fred barney betty wilma dino ) Perl 将它们当作单引号字符串处理,不能像双引号那样在qw 中使用(\n) 和($fred)(运行例子)。

12 qw 简写 qw(fred barney betty
wilma dino) #qw自动忽略空白(空格、制表符、换行符), 剩下的就是列表的元素 qw ( fred barney betty wilma dino ) #推荐写成这样

13 qw 简写 qw ! fred barney betty wilma dino !
qw !Yahoo \! Google excite lycos ! qw (Yahoo \! Google excite lycos ) qw !Yahoo \\ Google excite lycos !

14 3.2列表相关操作--赋值 与标量值类似,列表值也可以赋给变量:
($fred, $barney, $dino) = (“flintstone”, “rubble”, undef); ($fred, $barney) = ($barney, $fred) #交换两个变量 ($betty[0],$betty[1]) = ($betty[1],$betty[0]); ($fred, $barney) = qw <flintstone rubble slate>; #一个值被忽略了 ($wilma,$dino) = qw[flintstone]; #$dino 为undef ($rocks[0],$rocks[1],$rocks[2],$rocks[3] ) = qw/talc mica feldspar quartz/; @rocks = qw /talc mica feldspar quartz /;

15 列表的赋值 数组表示方式 @ @rocks = qw / bedrock slate lava /; @tiny = (); #空表
@giant = 1..1e5; #包含100,000 个元素的表 @stuff = #包含200,001 个元素的表 @dino = “granite”; @quarry = “crushed $dino); @copy #将一个数组中的值拷贝的 另一个数组中

16 pop 和push 操作 要新增元素到数组尾端,只要将它存在更高索引(index)对应的新位置。 一些相关的小技巧。 35 12.4
“hello” 1.72e30 “bye\n” 1 2 3 4 pop ‘new variable’ push 5

17 pop 和push 操作 pop 将数组的最后一个元素取出并返回:
@array = 5..9; $fred = #$fred 现在为 (5,6,7,8) $barney = #$barney gets 现在为 (5,6,7) 现在为(5,6)(7 被丢 弃了) 如果数组为空,那pop 什么也不做(因为没有元素可以移出),并返回undef。

18 pop 和push 操作 push,将一个元素(或一列元素)加在数组的末尾
现在为(5,6,0) 现在为(5,6,0,8) 现在多了10 个元素 @others =qw/ /; push 现在又多了5 个元素

19 shift 和unshift 操作 对一个数组的开头进行增减元素操作 35 12.4 “hello” 1.72e30 “bye\n” 1 2
‘new variable’ unshift 35 12.4 “hello” 1.72e30 “bye\n” 1 2 3 4 shift

20 shift 和unshift 操作 @array = qw # dino fred barney #;
$m = shift #$m 现在为(“fred”, “barney”) $n = #$n 现在为(“barney”) 现在为空 $o = #$o 仍为空 unshift ; 现在为(5) 现在为(4,5) @others = 1..3; unshift #array 现在为(1,2,3,4,5)

21 Splice操作符 对数组的中间元素进行操作(四个参赛) 参数1:要操作的目标数组 参数2:要操作的一组元素的开始位置
参数3:指定要操作的元素长度 参数4:要替换的列表 @array = qw{ pebbles dino fred barney betty}; @removed = 2; #在原来的数组中删除fred及其 pebbles dino ) @array变成( fred barney betty )(运行例子)

22 Splice操作符 @array = qw{ pebbles dino fred barney betty};
@removed = 1,2,qw(wilma); @removed = 1,0,qw(wilma);

23 将数组插入字符串 双引号内的数组变量内插 @rocks = qw{ flintstone slate rubble };
print limestone\n”; #输出为5 种rocks 由空格分开, print “Three rocks are: @rocks.\n”; print “Three rocks print “There’s nothing in the parens here..\n”

24 将数组插入字符串 当把email 地址插入双引号字符串时可能出现意想不到的结果
$ $ #正确 $ #另一种方法

25 将数组插入字符串 除了整个数组可以进行字符串内插,单个数组变量也可以 @fred = qw(hello dolly); $y = 2;
$x =“This is $fred[1]’s place”; # “This is dolly’s place” $x =“This is $fred[$y-1]’s place”; #同上

26 将数组插入字符串 一个标量变量后接一个左中括号符, @fred = qw(eating rocks is wrong);
$fred = “right”; #我们将打印“this is right[3]” print “this is $fred[3]\n”; #打印出“wrong”使用$fred[3] print “this is ${fred}[3]\n”; #打印出“right”(由花括号分开) print “this is $fred”. “[3]\n”; #正确(两个字符串,右. 分开) print “this is $fred\[3]\n”; #正确(利用反斜线)

27 3.3 foreach 控制结构 foreach 从列表的第一个元素一直循环执行到最后一个元素,一次迭代一个:
foreach $rock (qw / bedrock slate lava / ) { print “One rock is $rock.\n”; #打印出3 种rocks }

28 3.3 foreach 控制结构 控制变量(本例中为$rock)每次迭代从列表中取一个新值;控制变量不是元素拷贝而是元素本身。如果循环中修改该变量,原始列表中的元素也会被修改; @rocks = qw/ bedrock slate lava /; foreach $rock = “\t$rock”; 的每一个元素前加入一个tab $rock . = “\n”; #每一个元素后加一个换行符 } print “The rocks #每一个元素都被缩进了,并且一个元素占一行 (运行例子)

29 Perl 常用默认变量:$_ foreach 循环中省略了控制变量,那Perl 会使用其默认的变量:$_。
print “I can count to $_!\n”; } $_ =“Yabba dabba doo\n”; print; #打印出默认变量$_。

30 reverse 操作 reverse(逆转)操作将输入的一串列表(可能是数组)按相反的顺序返回 @fred = 6 ..10;
@barney = reverse #得到10,9,8,7,6 @wilma = reverse 6 ..10; #同上,没有使用额外的数组 @fred = #将逆转过的字符串存回去 的值 @fred = # 的值

31 sort 操作 sort 将输入的一串列表根据内部的字符顺序进行排序
@rocks = qw/ bedrock slate rubble granite /; @sorted = #得到bedrock, granite, rubble, slate @back = reverse #为slate 到bedrock @rocks = @numbers = sort 97 ..102; #得到100,101,102,97,98,99 @rocks = 值是经过排序的

32 3.4 标量和列表上下文 一个给定的表达式在不同的上下文中其含义是不同的。 42 + something #something 必须是标量
sort something #something 必须是列表 @people = qw( fred barney betty ); @sorted = #列表context:barney , betty, fred $number = #标量context:42+3,得到45

33 3.4 标量和列表上下文 强制引入标量 Print “I have”,scalar @rocks,“\n”;
@rocks = qw/ bedrock slate rubble granite /; Print “I 列表上下文中的<STDIN> @lines=<STDIN>;#从键盘输入的时候以ctr+z

34 本章小结 掌握:数组的基本操作和foreach 熟悉:基本函数如pop push…

35 Thank You !


Download ppt "Perl 语言."

Similar presentations


Ads by Google