?? 第二章 簡單變量.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb_2312-80">
<title></title>
<LINK rel="stylesheet" href="article.css" type="text/css">
</head>
<body>
<br>
<p align="center"><strong>第二章 簡單變量 </strong><br></p>
<p align="center"><a target="_blank" href="E:\緩沖區\Perl 5 教程">
by flamephoenix</a></p>
<a href="#1、整型">一、整型</a>
<br>
<a href="#二、浮點數">二、浮點數</a>
<br>
<a href="#三、字符串">三、字符串</a></p>
<p> 基本上,簡單變量就是一個數據單元,這個單元可以是數字或字符串。<br>
<a name="1、整型">一、整型</a>
<br>
1、整型 <br>
PERL最常用的簡單變量,由于其與其它語言基本相同,不再贅述。<br>
例:<br>
$x = 12345;<br>
if (1217 + 116 == 1333) {<br>
# statement block goes here<br>
}<br>
整型的限制:<br>
PERL實際上把整數存在你的計算機中的浮點寄存器中,所以實際上被當作浮點數看待。在多數計算機中,浮點寄存器可以存貯約16位數字,長于此的被丟棄。整數實為浮點數的特例。<br>
2、8進制和16進制數<br>
8進制以0打頭,16進制以0x打頭。<br>
例:$var1 = 047; (等于十進制的39)<br>
$var2 = 0x1f; (等于十進制的31)<br>
<a name="二、浮點數">二、浮點數</a>
<br>
如 11.4 、 -0.3 、.3 、
3. 、 54.1e+02 、 5.41e03<br>
浮點寄存器通常不能精確地存貯浮點數,從而產生誤差,在運算和比較中要特別注意。指數的范圍通常為-309到+308。<br>
例:<br>
<br>
#!/usr/local/bin/perl<br>
$value = 9.01e+21 + 0.01 - 9.01e+21;<br>
print ("first value is ", $value,
"\n");<br>
$value = 9.01e+21 - 9.01e+21 + 0.01;<br>
print ("second value is ", $value,
"\n");<br>
<br>
---------------------------------------------------------<br>
<br>
$ program3_3<br>
first value is 0<br>
second value is 0.01<br>
<a name="三、字符串">三、字符串</a>
<br>
慣用C的程序員要注意,在PERL中,字符串的末尾并不含有隱含的NULL字符,NULL字符可以出現在串的任何位置。<br>
.
雙引號內的字符串中支持簡單變量替換,例如:<br>
$number = 11;<br>
$text = "This text contains the number
$number.";<br>
則$text的內容為:"This text contains the number
11."<br>
<br>
.雙引號內的字符串中支持轉義字符<br>
<b>Table 3.1. Escape sequences in strings.</b> </p>
<div align="center"><center>
<table border="1" width="60%">
<tr>
<td valign="top" width="145"> <b>Escape
Sequence</b></td>
<td valign="top" width="335"> <b>Description</b></td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\a</tt></td>
<td valign="top" width="335"> Bell
(beep) </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\b</tt></td>
<td valign="top" width="335"> Backspace
</td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\cn</tt></td>
<td valign="top" width="335"> The
Ctrl+<tt>n</tt> character </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\e</tt></td>
<td valign="top" width="335"> Escape
</td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\E</tt></td>
<td valign="top" width="335"> Ends
the effect of <tt>\L</tt>, <tt>\U</tt> or <tt>\Q</tt> </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\f</tt></td>
<td valign="top" width="335"> Form
feed </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\l</tt></td>
<td valign="top" width="335"> Forces
the next letter into lowercase </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\L</tt></td>
<td valign="top" width="335"> All
following letters are lowercase </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\n</tt></td>
<td valign="top" width="335"> Newline
</td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\r</tt></td>
<td valign="top" width="335"> Carriage
return </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\Q</tt></td>
<td valign="top" width="335"> Do not
look for special pattern characters </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\t</tt></td>
<td valign="top" width="335"> Tab </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\u</tt></td>
<td valign="top" width="335"> Force
next letter into uppercase </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\U</tt></td>
<td valign="top" width="335"> All
following letters are uppercase </td>
</tr>
<tr>
<td valign="top" width="145"> <tt>\v</tt></td>
<td valign="top" width="335"> Vertical
tab </td>
</tr>
</table>
</center></div>
<p align="left"> \L、\U、\Q功能可以由\E關閉掉,如:<br>
$a = "T\LHIS IS A \ESTRING"; # same as "This
is a STRING"<br>
<br>
.要在字符串中包含雙引號或反斜線,則在其前加一個反斜線,反斜線還可以取消變量替換,如:<br>
$res = "A quote \" and A backslash
\\";<br>
$result = 14;<br>
print ("The value of \$result is
$result.\n")的結果為:<br>
The value of $result is 14.<br>
<br>
.可用\nnn(8進制)或\xnn(16進制)來表示ASCII字符,如:<br>
$result = "\377"; # this is the character
255,or EOF<br>
$result = "\xff"; # this is also 255<br>
<br>
.單引號字符串<br>
單引號字符串與雙引號字符串有兩個區別,一是沒有變量替換功能,二是反斜線不支持轉義字符,而只在包含單引號和反斜線時起作用。單引號另一個特性是可以跨多行,如:<br>
$text = 'This is two<br>
lines of text<br>
'; <br>
與下句等效:<br>
$text = "This is two\nlines of text\n"; <br>
<br>
.字符串和數值的互相轉換<br>
例1:<br>
$string = "43";<br>
$number = 28;<br>
$result = $string + $number; # $result = 71<br>
若字符串中含有非數字的字符,則從左起至第一個非數字的字符,如:<br>
$result = "hello" * 5; # $result = 0<br>
$result = "12a34" +1; # $result = 13<br>
<br>
.變量初始值<br>
在PERL中,所有的簡單變量都有缺省初始值:"",即空字符。但是建議給所有變量賦初值,否則當程序變得大而復雜后,很容易出現不可預料且很難調試的錯誤。<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 + -