perl -v
2.注意:
註解 #
單行結束 ;
純量用 $ 開頭
純量陣列用@開頭
雜湊陣列用%開頭
用 my 或 local 宣告區域變數,反之為全域
字串雙引號夾注 "字串" ,數值不用引號
Perl只定義了一維陣列的語法,二維以上只能用指標間接來達成。
0 是false、非0 是ture
3.資料型態大致分為四種:Scalar純量、Scalar Array純量陣列、Hash Array雜湊、References指標
單數為scalar
複數為array
陣列個數 $#array
雜湊就是索引值不是數值的陣列
純量陣列宣告:
(1)
my @array=qw(a b c d);
(2)
my @array=("a","b","c","d");
(3)
$array[0]="a"; $array[1]="b"; $array[2]="c"; $array[3]="d";
雜湊陣列宣告:
(1)
my %hash=("i1"=>"aaa","i2"=>"bbb","i3"=>"ccc");
(2)
my %hash=("i1","aaa","i2","bbb","i3","ccc");
(3)
$hash{'i1'}="aaa"; $hash{'i2'}="bbb"; $hash{'i3'}="ccc";
下面是使用相關陣列的三個例子:
(1)
foreach $key (keys %hash) {
print "$hash{$key}\n";
}
(2)
foreach $value (values %hash)
(3)
while(($key,$value)=each %hash)
4.字串運算子
字串比較的運算子有:
運算子 | 目的 |
x | 返回 x 左側字串再重複 x 右側的次數。如: "測試"x3,返回 "測試測試測試" |
. | 連接 . 兩側的字串。如:"測試"."字串",返回 "測試字串" |
eq | 如果兩邊相等,返回 true ,反之,返回 false,即「==」 |
ne | 如果兩邊不相等,返回 true ,反之,返回 false,即「!=」 |
le | 如果左側小於或等於右側,返回 true ,反之,返回 false,即「<=」 |
lt | 如果左側小於右側,返回 true ,反之,返回 false,即「<」 |
ge | 如果左側大於或等於右側,返回 true ,反之,返回 false,即「>=」 |
gt | 如果左側大於右側,返回 true ,反之,返回 false,即「>」 |
cmp | 如果左側的字串小於、等於或大於右側,則返回-1,0或1。 |
, | Evaluates the left operand, the evaluates the right operand. It returns the result of the right operand. |
++ | 將字串增加一個字母值 |
5.數值運算子
數值運算子 | 目的 |
+ | Computes the additive value of the two operands. |
- | Computes the difference between the two operands. |
* | Computes the multiplication of the two operands. |
/ | Computes the division between the two operands. |
% | Computes the modulus(remainder) of the two operands. |
= = | Returns Ture if the two operands are equivalent, False otherwise. |
!= | Returns Ture if the two operands are not equal, False otherwise. |
<= | Returns Ture if the operand on the left is numerically less than or equal to the operand on the right of the operator. Returns False otherwise. |
=> | Returns Ture if the operand on the left is numerically greater than or equal to the operand on the right of the operator. Returns False otherwise. |
< | Returns Ture if the operand on the left is numerically less than the operand on the right of the operator. Returns False otherwise. |
> | Returns Ture if the operand on the left is numerically greater than the operand on the right of the operator. Returns False otherwise. |
< = > | Returns -1 if the left operand is less than the right, +1 if is it greater than, and 0(False) otherwise. |
&& | Performs a logical AND operation. If the left operand is True m then the right operator is not evaluated. |
|| | Performs a logical OR operation. If the left operand is True m then the right operator is not evaluated. |
& | Returns the valueof the two operators bitwise ANDed. |
| | Returns the valueof the two operators bitwise ORed. |
^ | Returns the valueof the two operators bitwise XORed. |
++ | Increment operator. Increments the variable's value by 1. |
-- | Decrement operator. Decrements the variable's value by 1. |
** | Computes the power of the left-hand value to the power of the rihght-hand value. |
+= | Adds the value of the right-hand operand to the value of the left-hand operand. |
-+ | Subtracts the value of the right-hand operand to the value of the left-hand operand. |
*= | Mlutiplies the value of the left-hand operand to the value of the right-hand operand. |
>> | Shifts the left operand right by the number of bits that is specified by the right operand. |
<< | Shifts the left operand left by the number of bits that is specified by the right operand. |
~ | Performs a 1s complement of the operator. This is a unary operator. |
6.控制流程
(1)if
if (條件) {程式碼}
if (條件) {程式碼} else {程式碼}
if (條件) {程式碼} elsif (程式碼) {程式碼} else {程式碼}
(2)for
for($i=0; $i<=10; $i++) {程式碼}
for $i (0..10) {程式碼}
(3)foreach
for($i=0; $i<=$#array; $i++)
foreach $i (@array) {程式碼}
(4)while和do
while($i<=10) {程式碼}
do {Code Segment} while(程式碼);
(5)last是跳出現在所在的迴圈,next則是跳過下面的指令直接執行下一次的迴圈。
while(chomp($i=<STDIN>)) {
next if ($i == 5);
last unless ($i > 10);
}
參考資料:
http://ind.ntou.edu.tw/~dada/cgi/Perlsynx.htm
沒有留言:
張貼留言