⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perl3.htm

📁 Perl入门教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<br>
&nbsp;&nbsp;$stringvar = &quot;AGZZZ&quot;;<br>
&nbsp;&nbsp;$stringvar++; # $stringvar now contains
&quot;AHAAA&quot; <br>
<br>
&nbsp;&nbsp;$stringvar = &quot;ab4&quot;;<br>
&nbsp;&nbsp;$stringvar++; # $stringvar now contains
&quot;ab5&quot;<br>
<br>
&nbsp;&nbsp;$stringvar = &quot;bc999&quot;;<br>
&nbsp;&nbsp;$stringvar++; # $stringvar now contains
&quot;bd000&quot; <br>
.不要使用--,PERL将先将字符串转换为数字再进行自减<br>
&nbsp;&nbsp;$stringvar = &quot;abc&quot;;<br>
&nbsp;&nbsp;$stringvar--; # $stringvar = -1 now<br>
<br>
.如果字符串中含有非字母且非数字的字符,或数字位于字母中,则经过++运算前值转换为数字零,因此结果为1,如:<br>
&nbsp;&nbsp;$stringvar = &quot;ab*c&quot;;<br>
&nbsp;&nbsp;$stringvar++;<br>
&nbsp;&nbsp;$stringvar = &quot;ab5c&quot;;<br>
&nbsp;&nbsp;$stringvar++; <br>
<a name="八、字符串联结和重复操作符">
八、字符串联结和重复操作符</a>
<br>
&nbsp;&nbsp;联接: .<br>
&nbsp;&nbsp;重复:x<br>
&nbsp;&nbsp;联接且赋值(类似+=): .=<br>
例:<br>
&nbsp;&nbsp;$newstring = &quot;potato&quot; . &quot;head&quot;;<br>
&nbsp;&nbsp;$newstring = &quot;t&quot; x 5;<br>
&nbsp;&nbsp;$a = &quot;be&quot;;<br>
&nbsp;&nbsp;$a .= &quot;witched&quot;; # $a is now
&quot;bewitched&quot;<br>
<a name="九、逗号操作符"> 九、逗号操作符</a>
<br>
&nbsp;&nbsp;其前面的表达式先进行运算,如:<br>
&nbsp;&nbsp;$var1 += 1, $var2 = $var1;<br>
&nbsp;&nbsp;等价于<br>
&nbsp;&nbsp;$var1 += 1;<br>
&nbsp;&nbsp;$var2 = $var1;<br>
&nbsp;&nbsp;使用此操作符的唯一理由是提高程序的可读性,将关系密切的两个表达式结合在一起,如:<br>
&nbsp;&nbsp;$val = 26;<br>
&nbsp;&nbsp;$result = (++$val, $val + 5); # $result = 32<br>
&nbsp;&nbsp;注意如果此处没有括号则意义不同:<br>
&nbsp;&nbsp;$val = 26;<br>
&nbsp;&nbsp;$result = ++$val, $val + 5; # $result = 27<br>
<a name="十、条件操作符"> 十、条件操作符</a>
<br>
&nbsp;&nbsp;与C中类似,条件?值1:值2,当条件为真时取值1,为假时取值2,如:<br>
&nbsp;&nbsp;$result = $var == 0 ? 14 : 7;<br>
&nbsp;&nbsp;$result = 43 + ($divisor == 0 ? 0 : $dividend /
$divisor);<br>
&nbsp;&nbsp;PERL 5中,还可以在赋值式左边使用条件操作符来选择被赋值的变量,如:<br>
&nbsp;&nbsp;$condvar == 43 ? $var1 : $var2 = 14;<br>
&nbsp;&nbsp;$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>&lt;&lt;</tt>,
        <tt>&gt;&gt;</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>&lt;</tt>,
        <tt>&lt;=</tt>, <tt>&gt;</tt>, <tt>&gt;=</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>&lt;=&gt;</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>&amp;</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>&amp;&amp;</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>&lt;&lt;</tt>,
        <tt>&gt;&gt;</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>&lt;</tt>,
        <tt>&lt;=</tt>, <tt>&gt;</tt>, <tt>&gt;=</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>&lt;=&gt;</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>&amp;</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>&amp;&amp;</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>
&nbsp;&nbsp;1、当你不确定某操作符是否先执行时,一定要用括号明确之。<br>
&nbsp;&nbsp;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 + -