?? 第三章 操作符.htm
字號:
<br>
$stringvar = "AGZZZ";<br>
$stringvar++; # $stringvar now contains
"AHAAA" <br>
<br>
$stringvar = "ab4";<br>
$stringvar++; # $stringvar now contains
"ab5"<br>
<br>
$stringvar = "bc999";<br>
$stringvar++; # $stringvar now contains
"bd000" <br>
.不要使用--,PERL將先將字符串轉換為數字再進行自減<br>
$stringvar = "abc";<br>
$stringvar--; # $stringvar = -1 now<br>
<br>
.如果字符串中含有非字母且非數字的字符,或數字位于字母中,則經過++運算前值轉換為數字零,因此結果為1,如:<br>
$stringvar = "ab*c";<br>
$stringvar++;<br>
$stringvar = "ab5c";<br>
$stringvar++; <br>
<a name="八、字符串聯結和重復操作符">
八、字符串聯結和重復操作符</a>
<br>
聯接: .<br>
重復:x<br>
聯接且賦值(類似+=): .=<br>
例:<br>
$newstring = "potato" . "head";<br>
$newstring = "t" x 5;<br>
$a = "be";<br>
$a .= "witched"; # $a is now
"bewitched"<br>
<a name="九、逗號操作符"> 九、逗號操作符</a>
<br>
其前面的表達式先進行運算,如:<br>
$var1 += 1, $var2 = $var1;<br>
等價于<br>
$var1 += 1;<br>
$var2 = $var1;<br>
使用此操作符的唯一理由是提高程序的可讀性,將關系密切的兩個表達式結合在一起,如:<br>
$val = 26;<br>
$result = (++$val, $val + 5); # $result = 32<br>
注意如果此處沒有括號則意義不同:<br>
$val = 26;<br>
$result = ++$val, $val + 5; # $result = 27<br>
<a name="十、條件操作符"> 十、條件操作符</a>
<br>
與C中類似,條件?值1:值2,當條件為真時取值1,為假時取值2,如:<br>
$result = $var == 0 ? 14 : 7;<br>
$result = 43 + ($divisor == 0 ? 0 : $dividend /
$divisor);<br>
PERL 5中,還可以在賦值式左邊使用條件操作符來選擇被賦值的變量,如:<br>
$condvar == 43 ? $var1 : $var2 = 14;<br>
$condvar == 43 ? $var1 = 14 : $var2 = 14;<br>
<a name="十一、操作符的次序">
十一、操作符的次序</a>
<br>
<br>
</p>
<p align="center"> <b>Table 3.6.</b>
操作符次序</p>
<div align="center"><center>
<table border="1" width="65%">
<tr>
<td valign="top" width="214"> <b>操作符</b></td>
<td valign="top" width="310"> <b>描述</b></td>
</tr>
<tr>
<td valign="top" width="214"> <tt>++</tt>,
<tt>--</tt> </td>
<td valign="top" width="310"> 自增,自減</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>-</tt>,
<tt>~</tt>, <tt>!</tt> </td>
<td valign="top" width="310"> 單目</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>**</tt>
</td>
<td valign="top" width="310"> 乘方</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>=~</tt>,
<tt>!~</tt> </td>
<td valign="top" width="310"> 模式匹配</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>*</tt>,
<tt>/</tt>, <tt>%</tt>, <tt>x</tt> </td>
<td valign="top" width="310"> 乘,除,取余,重復</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>+</tt>,
<tt>-</tt>, <tt>.</tt> </td>
<td valign="top" width="310"> 加,減,聯接</td>
</tr>
<tr>
<td valign="top" width="214"> <tt><<</tt>,
<tt>>></tt> </td>
<td valign="top" width="310"> 移位</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>-e</tt>,
<tt>-r</tt>, etc. </td>
<td valign="top" width="310"> 文件狀態</td>
</tr>
<tr>
<td valign="top" width="214"> <tt><</tt>,
<tt><=</tt>, <tt>></tt>, <tt>>=</tt>, <tt>lt</tt>,
<tt>le</tt>, <tt>gt</tt>, <tt>ge</tt> </td>
<td valign="top" width="310"> 不等比較</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>==</tt>,
<tt>!=</tt>, <tt><=></tt>, <tt>eq</tt>, <tt>ne</tt>,
<tt>cmp</tt> </td>
<td valign="top" width="310"> 相等比較</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>&</tt>
</td>
<td valign="top" width="310"> 位與</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>|</tt>,
<tt>^</tt> </td>
<td valign="top" width="310"> 位或,位異或</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>&&</tt>
</td>
<td valign="top" width="310"> 邏輯與</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>||</tt>
</td>
<td valign="top" width="310"> 邏輯或</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>..</tt>
</td>
<td valign="top" width="310"> 列表范圍</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>?</tt>
and <tt>:</tt> </td>
<td valign="top" width="310"> 條件操作符</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>=</tt>,
<tt>+=</tt>, <tt>-=</tt>, <tt>*=</tt>, </td>
<td valign="top" width="310"> 賦值</td>
</tr>
<tr>
<td valign="top" width="214"> and so
on</td>
<td valign="top" width="310"> </td>
</tr>
<tr>
<td valign="top" width="214"> <tt>,</tt></td>
<td valign="top" width="310"> 逗號操作符</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>not</tt>
</td>
<td valign="top" width="310"> Low-precedence
logical NOT</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>and</tt>
</td>
<td valign="top" width="310"> Low-precedence
logical AND</td>
</tr>
<tr>
<td valign="top" width="214"> <tt>or</tt>,
<tt>xor</tt> </td>
<td valign="top" width="310"> Low-precedence
logical OR and XOR</td>
</tr>
</table>
</center></div>
<p> .操作符結合性(associativity):<br>
</p>
<p align="center"> <b>Table 3.7.
操作符結合性</b></p>
<div align="center"><center>
<table border="1" width="65%">
<tr>
<td valign="top" width="240"> <b>操作符</b></td>
<td valign="top" width="240"> <b>結合性</b></td>
</tr>
<tr>
<td valign="top" width="240"> <tt>++</tt>,
<tt>--</tt> </td>
<td valign="top" width="240"> 無</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>-</tt>,
<tt>~</tt>, <tt>!</tt> </td>
<td valign="top" width="240"> Right-to-left</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>**</tt>
</td>
<td valign="top" width="240"> Right-to-left</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>=~</tt>,
<tt>!~</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>*</tt>,
<tt>/</tt>, <tt>%</tt>, <tt>x</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>+</tt>,
<tt>-</tt>, <tt>.</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt><<</tt>,
<tt>>></tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>-e</tt>,
<tt>-r</tt>, </td>
<td valign="top" width="240"> 無</td>
</tr>
<tr>
<td valign="top" width="240"> <tt><</tt>,
<tt><=</tt>, <tt>></tt>, <tt>>=</tt>, <tt>lt</tt>,
<tt>le</tt>, <tt>gt</tt>, <tt>ge</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>==</tt>,
<tt>!=</tt>, <tt><=></tt>, <tt>eq</tt>, <tt>ne</tt>,
<tt>cmp</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>&</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>|</tt>,
<tt>^</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>&&</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>||</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>..</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>?</tt>
and <tt>:</tt> </td>
<td valign="top" width="240"> Right-to-left</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>=</tt>,
<tt>+=</tt>, <tt>-=</tt>, <tt>*=</tt>, </td>
<td valign="top" width="240"> Right-to-left</td>
</tr>
<tr>
<td valign="top" width="240"> and so
on</td>
<td valign="top" width="240"> </td>
</tr>
<tr>
<td valign="top" width="240"> <tt>,</tt></td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>not</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>and</tt>
</td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
<tr>
<td valign="top" width="240"> <tt>or</tt>,
<tt>xor</tt> </td>
<td valign="top" width="240"> Left-to-right</td>
</tr>
</table>
</center></div>
<p> <br>
<br>
建議:<br>
1、當你不確定某操作符是否先執行時,一定要用括號明確之。<br>
2、用多行、空格等方式提高程序的可讀性。<br>
</p>
<p align="center"><a href="第二章 簡單變量.htm">上一章</a> <a href="第四章 列表和數組變量 .htm">下一章</a> <a href="index.htm">目錄</a></p>
<br>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -