📄 perl3.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="perl2.htm">上一章</a> <a href="perl4.htm">下一章</a> <a href="index.htm">目录</a></p>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -