unx16.htm
来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,814 行 · 第 1/5 页
HTM
2,814 行
<BR></P>
<P>You can use as many sets of parentheses as you like:
<BR></P>
<PRE>$x = 4 ** (5 % (8 - 6));</PRE>
<P>Here, the result is 4:
<BR></P>
<UL>
<LI>8 - 6 is performed, leaving 4 ** (5 % 2)
<BR>
<BR></LI>
<LI>5 % 2 is performed, leaving 4 ** 1
<BR>
<BR></LI>
<LI>4 ** 1 is 4
<BR>
<BR></LI></UL>
<H3 ALIGN="CENTER">
<CENTER><A ID="I31" NAME="I31">
<FONT SIZE=4><B>Using Lists and Array Variables</B>
<BR></FONT></A></CENTER></H3>
<P>So far, the Perl programs you have seen have only used scalar data and scalar variables. In other words, they have only dealt with one value at a time.
<BR></P>
<P>Perl also allows you to manipulate groups of values, known as lists or arrays. These lists can be assigned to special variables known as array variables, which can be processed in a variety of ways.
<BR></P>
<P>This section describes lists and array variables, and how to use them. It also describes how to pass command-line arguments to your program using the special-purpose array @ARGV.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I32" NAME="I32">
<FONT SIZE=3><B>Introducing Lists</B>
<BR></FONT></A></CENTER></H4>
<P>A list is a collection of scalar values enclosed in parentheses. The following is a simple example of a list:
<BR></P>
<PRE>(1, 5.3, "hello", 2)</PRE>
<P>This list contains four elements, each of which is a scalar value: the numbers 1 and 5.3, the string "hello", and the number 2. As always in Perl, numbers and character strings are interchangeable: each element of a list can be either a number
or a string.
<BR></P>
<P>A list can contain as many elements as you like (or as many as your machine's memory can store at one time). To indicate a list with no elements, just specify the parentheses:
<BR></P>
<PRE>() # this list is empty</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I33" NAME="I33">
<FONT SIZE=3><B>Scalar Variables and Lists</B>
<BR></FONT></A></CENTER></H5>
<P>Lists can also contain scalar variables:
<BR></P>
<PRE>(17, $var, "a string")</PRE>
<P>Here, the second element of the list is the scalar variable $var. When Perl sees a scalar variable in a list, it replaces the scalar variable with its current value.
<BR></P>
<P>A list element can also be an expression:
<BR></P>
<PRE>(17, $var1 + $var2, 26 << 2)</PRE>
<P>Here, the expression $var1 + $var2 is evaluated to become the second element, and the expression 26 << 2 is evaluated to become the third element.
<BR></P>
<P>Scalar variables can also be replaced in strings:
<BR></P>
<PRE>(17, "the answer is $var1")</PRE>
<P>In this case, the value of $var1 is placed into the string.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I34" NAME="I34">
<FONT SIZE=3><B>Using List Ranges</B>
<BR></FONT></A></CENTER></H5>
<P>Suppose that you want to define a list consisting of the numbers 1 through 10, inclusive. You can do this by typing in each of the numbers in turn:
<BR></P>
<PRE>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)</PRE>
<P>However, there is a simpler way to do it: use the list range operator, which is .. (two consecutive periods). The following is a list created using the list range operator:
<BR></P>
<PRE>(1..10)</PRE>
<P>This tells Perl to define a list whose first value is 1, whose second value is 2, and so on up to 10.
<BR></P>
<P>The list range operator can be used to define part of a list:
<BR></P>
<PRE>(2, 5..7, 11)</PRE>
<P>This list consists of five elements: the numbers 2, 5, 6, 7 and 11.
<BR></P>
<P>List range operators can also be used with floating-point values:
<BR></P>
<PRE>(2.1..5.3)</PRE>
<P>This list consists of four elements: 2.1, 3.1, 4.1, and 5.1. Each element of the list is one greater than the previous element, and the last element of the list is the largest possible number less than or equal to the number to the right of the ..
operator. (If the value to the left of the .. operator is greater than the value to the right, an empty list is created.)
<BR></P>
<P>Elements that define the range of a list range operator can be expressions, and these expressions can contain scalar variables:
<BR></P>
<PRE>($a..$b+5)</PRE>
<P>This list consists of all values between the current value of $a and the current value of the expression $b+5.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I35" NAME="I35">
<FONT SIZE=3><B>Storing Lists in Array Variables</B>
<BR></FONT></A></CENTER></H4>
<P>Perl allows you to store lists in special variables designed for that purpose. These variables are called array variables.
<BR></P>
<P>The following is an example of a list being assigned to an array variable:
<BR></P>
<PRE>@array = (1, 2, 3);</PRE>
<P>Here, the list (1, 2, 3) is assigned to the array variable @array.
<BR></P>
<P>Note that the name of the array variable starts with the character @. This allows Perl to distinguish array variables from other kinds of variables, such as scalar variables, which start with the character $. As with scalar variables, the second
character of the variable name must be a letter, and subsequent characters of the name can be letters, numbers, or underscores.
<BR></P>
<P>When an array variable is first created (seen for the first time), it is assumed to contain the empty list () unless something is assigned to it.
<BR></P>
<P>Because Perl uses @ and $ to distinguish array variables from string variables, the same name can be used in an array variable and in a string variable:
<BR></P>
<PRE>$var = 1;
@var = (11, 27.1, "a string");</PRE>
<P>Here, the name var is used in both the string variable $var and the array variable @var. These are two completely separate variables.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I36" NAME="I36">
<FONT SIZE=3><B>Assigning to Array Variables</B>
<BR></FONT></A></CENTER></H4>
<P>As you have already seen, lists can be assigned to array variables with the assignment operator =:
<BR></P>
<PRE>@x = (11, "my string", 27.44);</PRE>
<P>You can also assign one array variable to another:
<BR></P>
<PRE>@y = @x;</PRE>
<P>A scalar value can be assigned to an array variable:
<BR></P>
<PRE>@x = 27.1;
@y = $x;</PRE>
<P>In this case, the scalar value (or value stored in a scalar variable) is converted into a list containing one element.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I37" NAME="I37">
<FONT SIZE=3><B>Using Array Variables in Lists</B>
<BR></FONT></A></CENTER></H5>
<P>As you have already seen, lists can contain scalar variables:
<BR></P>
<PRE>@x = (1, $y, 3);</PRE>
<P>Here, the value of the scalar variable $y becomes the second element of the list assigned to @x.
<BR></P>
<P>You can also specify that the value of an array variable is to appear in a list:
<BR></P>
<PRE>@x = (2, 3, 4);
@y = (1, @x, 5);</PRE>
<P>Here, the list (2, 3, 4) is substituted for @x, and the resulting list (1, 2, 3, 4, 5) is assigned to @y.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I38" NAME="I38">
<FONT SIZE=3><B>Assigning to Scalar Variables from Array Variables</B>
<BR></FONT></A></CENTER></H5>
<P>Consider the following assignment:
<BR></P>
<PRE>@x = ($a, $b);</PRE>
<P>Here, the values of the scalar variables $a and $b are used to form a two-element list that is assigned to the array variable @x.
<BR></P>
<P>Perl also allows you to take the current value of an array variable and assign its components to a group of scalar variables:
<BR></P>
<PRE>($a, $b) = @x;</PRE>
<P>Here, the first element of the list currently stored in @x is assigned to $a, and the second element is assigned to $b. Additional elements in @x, if they exist, are not assigned.
<BR></P>
<P>If there are more scalar variables than elements in an array variable, the excess scalar variables are given the value "" (the null string), which is equivalent to the numeric value 0:
<BR></P>
<PRE>@x = (1, 2);
($a, $b, $c) = @x; # $a is now 1, $b is now 2, $c is now ""</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I39" NAME="I39">
<FONT SIZE=3><B>Retrieving the Length of a List</B>
<BR></FONT></A></CENTER></H5>
<P>As you have already seen, when a scalar value is assigned to an array variable, the value is assumed to be a list containing one element. For example, the following statements are equivalent:
<BR></P>
<PRE>@x = $y;
@x = ($y);</PRE>
<P>However, the converse is not true. In the statement $y = @x;, the value assigned to $y is the number of elements in the list currently stored in @x:
<BR></P>
<PRE>@x = ("string 1", "string 2", "string 3");
$y = @x; # $y is now 3</PRE>
<P>To assign the value of the first element of a list to a scalar variable, enclose the scalar variable in a list:
<BR></P>
<PRE>@x = ("string 1", "string 2", "string 3");
($y) = @x; # $y is now "string 1" </PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I40" NAME="I40">
<FONT SIZE=3><B>Using Array Slices</B>
<BR></FONT></A></CENTER></H4>
<P>Perl allows you to specify what part of an array to use in an expression. The following example shows you how to do this:
<BR></P>
<PRE>@x = (1, 2, 3);
@y = @x[0,1];</PRE>
<P>Here, the list (1, 2, 3) is first assigned to the array variable @x. Then, the array slice [0,1] is assigned to @y: in other words, the first two elements of @x are assigned to @y. (Note that the first element of the array is specified by 0, not 1.)
<BR></P>
<P>You can assign to an array slice as well:
<BR></P>
<PRE>@x[0,1] = (11.5, "hello");</PRE>
<P>This statement assigns the value 11.5 to the first element of the array variable @x, and assigns the string "hello" to the second.
<BR></P>
<P>Array variables automatically grow when necessary, with null strings assigned to fill any gaps:
<BR></P>
<PRE>@x = (10, 20, 30);
@x[4,5] = (75, 85);</PRE>
<P>Here, the second assignment increases the size of the array variable @x from three elements to six, and assigns 75 to the fifth element and 85 to the sixth. The fourth element is set to be the null string.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I41" NAME="I41">
<FONT SIZE=3><B>Using Array Slices with Scalar Variables</B>
<BR></FONT></A></CENTER></H5>
<P>An array slice can consist of a single element. In this case, the array slice is treated as if it is a scalar variable:
<BR></P>
<PRE>@x = (10, 20, 30);
$y = $x[1]; # $y now has the value 20</PRE>
<P>Note that the array slice is now preceded by the character $, not the character @. This tells Perl that the array slice is to be treated as a scalar variable.
<BR></P>
<P>Recall that array variables and scalar variables can have the same name:
<BR></P>
<PRE>$x = "Smith";
@x = (47, "hello");</PRE>
<P>Here, the scalar variable $x and the array variable @x are both defined, and are completely independent of one another. This can cause problems if you want to include a scalar variable inside a string:
<BR></P>
<PRE>$y = "Refer to $x[1] for more information.";</PRE>
<P>In this case, Perl assumes that you want to substitute the value of the array slice $x[1] into the string. This produces the following:
<BR></P>
<PRE>$y = "Refer to hello for more information.";</PRE>
<P>To specify the scalar variable and not the array slice, enclose the variable name in braces:
<BR></P>
<PRE>$y = "Refer to ${x}[1] for more information.";</PRE>
<P>This tells Perl to replace $x, not $x[1], and produces the following:
<BR></P>
<PRE>$y = "Refer to Smith[1] for more information.";</PRE>
<H5 ALIGN="CENTER">
<CENTER><A ID="I42" NAME="I42">
<FONT SIZE=3><B>Using the Array Slice Notation as a Shorthand</B>
<BR></FONT></A></CENTER></H5>
<P>So far, we have been using the array slice notation @x[0,1] to refer to a portion of an array variable. As it happens, in Perl the above is exactly equivalent to a list of single-element array slices:
<BR></P>
<PRE>@y = @x[0,1];
@y = ($x[0], $x[1]); # these two statements are identical</PRE>
<P>This allows you to use the array slice notation whenever you want to refer to more than one element in an array:
<BR></P>
<PRE>@y = @x[4,1,5];</PRE>
<P>In this statement, the array variable @y is assigned the values of the fifth, second, and sixth elements of the array variable @x.
<BR></P>
<PRE>@y[0,1,2] = @x[1,1,1];</PRE>
<P>Here, the second element of @x is copied to the first three elements of @y.
<BR></P>
<P>In Perl, assignments in which the operands overlap are handled without difficulty. Consider this example:
<BR></P>
<PRE>@x[4,3] = @x[3,4];</PRE>
<P>Perl performs this assignment by creating a temporary array variable, copying @x[3,4] to it, and then copying it to @x[4,3]. Thus, this statement swaps the values in the fourth and fifth elements of @x.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I43" NAME="I43">
<FONT SIZE=3><B>Other Array Operations</B>
<BR></FONT></A></CENTER></H4>
<P>Perl provides a number of built-in functions that work on lists and array variables. For example, you can sort array elements in alphabetic order, reverse the elements of an array, remove the last character from all elements of an array, and merge the
elements of an array into a single string.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I44" NAME="I44">
<FONT SIZE=3><B>Sorting a List or Array Variable</B>
<BR></FONT></A></CENTER></H5>
<P>The built-in function sort() sorts the elements of an array in alphabetic order and returns the sorted list:
<BR></P>
<PRE>@x = ("this", "is", "a", "test");
@x = sort (@x); # @x is now ("a", "is", "test", "this")</PRE>
<P>Note that the sort is in alphabetic, not numeric, order:
<BR></P>
<PRE>@x = (70, 100, 8);
@x = sort (@x); # @x is now ("100", "70", "8")</PRE>
<P>The number 100 appears first because the string "100" is alphabetically ahead of "70" (since "1" appears before "7").
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I45" NAME="I45">
<FONT SIZE=3><B>Reversing a List or Array Variable</B>
<BR></FONT></A></CENTER></H5>
<P>The function reverse() reverses the order of the elements in a list or array variable and returns the reversed list:
<BR></P>
<PRE>@x = ("backwards", "is", "array", "this");
@x = reverse(@x); # @x is now ("this", "array", "is", "backwards")</PRE>
<P>You can sort and reverse the same list:
<BR></P>
<PRE>@x = reverse(sort(@x));</PRE>
<P>This produces a sort in reverse alphabetical order.
<BR></P>
<H5 ALIGN="CENTER">
<CENTER><A ID="I46" NAME="I46">
<FONT SIZE=3><B>Using </B><B><I>chop()</I></B><B> on Array Variables</B>
<BR></FONT></A></CENTER></H5>
<P>The chop() function can be used on array variables as well as scalar variables:
<BR></P>
<PRE>$a[0] = <STDIN>;
$a[1] = <STDIN&
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?