第1課時 列表與數組
1)直接列表
(12,’abc‘,3.14,True) # 這是一個直接量列表
2)都是字符串可以用qw
# 兩者等價,最好不要有內嵌變量 @a = qw(abc def ghi) @b = ('abc','def','ghi') @c = ()
3)范圍列表
@a = (1..10) @b = (1..10, 21..30) # 字符照樣可以使用范圍運算符 @list=(aa..zz); foreach (@list){ print "$_ "; }
4)初始化
($a, $b, $c) = qw(li zhi xin);
5)獲取數組元素
@name=qw(li zhi xin); print $name[0]; print $name[1]; print $name[$#name]; #最后一個索引對應的數組元素 print $name[-1]; #最后一個索引對應的數組元素
6)pop與push,shift與unshift
@array = (5..9); $end = pop @array; #9被從結尾取了出來 push @array, 10; #10被加入到結尾 $begin = shift @array; #5被從開頭取了出來 unshift @array, 11; #11被加入到開頭
7)splice操作
splice @arrays, $loccation, $length, $content #$location: 元素索引 #$length: 替換長度,0為插入操作 #$content:要替換的內容,字符串或列表
8)遍歷數組
@rocks = qw/ bed slate lave / foreach $rock (@rocks){ $rock = "\t$rock"; $rock .= "\n"; } #省略變量的遍歷方法 foreach (@rocks){ $_ = "\t$rock"; $_ .= "\n"; }
9)排列數組