1、純量變數是Perl裡最基本的一種資料型態,它可以代表一個字元、字串、整數、甚至浮點數,而Perl把它們都看成是一樣的東東!你甚至可以混著用,不可思議吧。标量标量 是Perl里最基本的一种资料型态,它可以代表一个字元、字串、整数、甚至浮点数,而Perl把它们都看成是一样的东东!你甚至可以混着用,不可思议吧。 例如:例如:# 井字號開頭的後面都是註解。# 純量變數以$開頭。# my 是一種宣告變數的方式,它可以使變數區域化。# 宣告變數時若不加my 或local 則Perl會把它當作全域變數使用。# 習慣上,我們會將字串用雙引號括起來,而數值就不用加引號。my $x=abc;my $x=123;m
2、y $x=4.56;那麼程式怎麼判斷這是數值還是字串呢? 其實不是程式判斷,而是你自己要判斷。那么程式怎么判断这是数值还是字串呢?其实不是程式判断,而是你自己要判断。 Perl分別提供了一堆運算子來處理數字和字串,你必須知道這個變數是數值或字串,才能使用個別的運算子來對變數做運算。 Perl分别提供了一堆运算子来处理数字和字串,你必须知道这个变数是数值或字串,才能使用个别的运算子来对变数做运算。 我分別列出字串運算子和數值運算子,好讓大家能區分它們的不同。我分别列出字串运算子和数值运算子,好让大家能区分它们的不同。字串運算子 字串运算子 String Operator String Opera
3、tor Purpose Purpose x x Returns a string consisting of the string on the left of the operand, repeated the number of times of the right operand. Returns a string consisting of the string on the left of the operand, repeated the number of times of the right operand. Concatenates the two strings on bo
4、th sides of the operator. Concatenates the two strings on both sides of the operator. eq eq Returns True if the two operands are equivalent, False otherwise. Returns True if the two operands are equivalent, False otherwise. ne ne Returns True if the two operands are not equal, False otherwise. Retur
5、ns True if the two operands are not equal, False otherwise. le le Returns True if the operand on the left is stringwise less than the operand on the right of the operator. Returns False otherwise. Returns True if the operand on the left is stringwise less than the operand on the right of the operato
6、r. Returns False otherwise. lt lt Returns True if the operand on the left is stringwise less than or equal to the operand on the right of the operator. Returns False otherwise. Returns True if the operand on the left is stringwise less than or equal to the operand on the right of the operator. Retur
7、ns False otherwise. ge ge Returns True if the operand on the left is stringwise greater than or equal to the operand on the right of the operator. Returns False otherwise. Returns True if the operand on the left is stringwise greater than or equal to the operand on the right of the operator. Returns
8、 False otherwise. gt gt Returns True if the operand on the left is stringwise greater than the operand on the right of the operator. Returns False otherwise. Returns True if the operand on the left is stringwise greater than the operand on the right of the operator. Returns False otherwise. cmp cmp
9、Returns -1, 0, or 1 if the left operand is stringwise less than, equal to, or greater than the right operand. Returns -1, 0, or 1 if the left operand is stringwise less than, equal to, or greater than the right operand. , , Evaluates the left operand, the evaluates the right operand. It returns the
10、result of the right operand. Evaluates the left operand, the evaluates the right operand. It returns the result of the right operand. + + Increments the string by one alphabetic value. Increments the string by one alphabetic value. 數值運算子 数值运算子 Value Operator Value Operator + + Computes the additive
11、value of the two operands. Computes the additive value of the two operands. - - Computes the difference between the two operands. Computes the difference between the two operands. * * Computes the multiplication of the two operands. Computes the multiplication of the two operands. / / Computes the d
12、ivision between the two operands. Computes the division between the two operands. % % Computes the modulus(remainder) of 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 a
13、re equivalent, False otherwise. != != Returns Ture if the two operands are not equal, False otherwise. Returns Ture if the two operands are not equal, 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
14、 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 greater than the operand on the right of the operator. Returns False otherwise.
15、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. Returns -1 if the left operand is less than the right, +1
16、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 AND operation. If the left operand is True m then the right operator is not evaluated. | | Performs a logical OR operation. I
17、f 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 ANDed. | | Returns the va
18、lueof the two operators bitwise ORed. Returns the valueof the two operators bitwise ORed. Returns the valueof the two operators bitwise XORed. Returns the valueof the two operators bitwise XORed. Increment operator. Increments the variables value by 1. Increment operator. Increments the variables va
19、lue by 1. - - Decrement operator. Decrements the variables value by 1. Decrement operator. Decrements the variable* * Computes the power of the left-hand value to the power of the rihght-hand value. Computes the power of the left-hand value to the power of the rihght-hand value. += += Adds the value
20、 of the right-hand operand to the value of the left-hand operand. 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. Subtracts the value of the right-hand operand to the value of t
21、he left-hand operand. *= *= Mlutiplies the value of the left-hand operand to the value of the right-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 t
22、he 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. Shifts the left operand left by the number of bits that is specified by the right operand. Performs a 1s complement of the op
23、erator. This is a unary operator. Performs a 1s complement of the operator. This is a unary operator. (b) Scalar Array: (b) Scalar Array:純量陣列,陣列內的每一個元素都是Scalar variable。纯量阵列,阵列内的每一个元素都是Scalar variable。 宣告及使用方式如下:宣告及使用方式如下:# 純量陣列以 開頭。my array;my array=qw(abcd);# qw 函數會將其後的每個元素用逗點隔開,效果就像下面這行。my array=(a,bcd);# 當然你也可以一個個元素宣告,下面就是存取每一個元素的方法。# 因為陣列中的每一個元素都是純量變數,所以要以$ 開頭, # 剛開始容易搞混,請注意。$array0= $array1= $array2= $array3=# 使用for loop印出陣列內每個元素的值。for($i=0; $i 這個符號是Perl5新增的,是為了相關陣列量身定做的, # 因為索引和元素值都是純量,若使用= 這個符號, # (索引=元素值) 兩兩對應,就不容易發生失誤。my %hash=(i1aaai2bbbi3ccc# 上面這行的效果和下面這行是一樣的。a
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1