📄 ch2.htm
字号:
a key and then using <TT><FONT FACE="Courier">$bm</FONT></TT>
as the index. We could have just as easily added the string 'Ascii
File' with this hard-coded statement:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$subscripts{'asc'} = 'Ascii File';</FONT></TT>
</BLOCKQUOTE>
<P>
Items in an associative array are referred to as items stored
in a <I>hash</I>, because this is the way items are stored internally.
Look at the output from line 15, which dumps out the associative
array items.
<P>
In line 17, the script uses a <TT><FONT FACE="Courier">foreach</FONT></TT>
statement to loop over the keys in the <TT><FONT FACE="Courier">%subscripts</FONT></TT>
array. The <TT><FONT FACE="Courier">keys()</FONT></TT> function
returns a list of keys for a given hash. The value of the item
at <TT><FONT FACE="Courier">$subscripts{$key}</FONT></TT> is assigned
to <TT><FONT FACE="Courier">$value</FONT></TT> at line 19. You
could combine lines 18 and 19 into one statement like this without
loss of meaning:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print "Key = $key, Value = $subscripts{$key}
\n";</FONT></TT>
</BLOCKQUOTE>
<P>
Using the keys alone did not list the contents of the <TT><FONT FACE="Courier">%subscripts</FONT></TT>
hash in the order we want. To sort the output, you should sort
the keys into the hash. This is shown in line 24. The <TT><FONT FACE="Courier">sort()</FONT></TT>
function takes a list of items and returns a text-sorted version.
The <TT><FONT FACE="Courier">foreach</FONT></TT> function takes
the output from the <TT><FONT FACE="Courier">sort()</FONT></TT>
function applied to the value returned by the <TT><FONT FACE="Courier">keys()</FONT></TT>
function. To sort in decreasing order, you can apply the <TT><FONT FACE="Courier">reverse</FONT></TT>
function to the returned value of <TT><FONT FACE="Courier">sort()</FONT></TT>
to get this line:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">for $i (reverse sort (keys %@array))
{</FONT></TT>
</BLOCKQUOTE>
<P>
It's more efficient to use the <TT><FONT FACE="Courier">each()</FONT></TT>
function when working with associative arrays because only one
lookup is required per item to get both the key and its value.
See Line 30 where the (<TT><FONT FACE="Courier">$key</FONT></TT>,<TT><FONT FACE="Courier">$value</FONT></TT>)
pairs are assigned to the returned values by the <TT><FONT FACE="Courier">each()</FONT></TT>
command. The variable <TT><FONT FACE="Courier">$key</FONT></TT>
is assigned to the first item, and the variable <TT><FONT FACE="Courier">$value</FONT></TT>
is assigned to the second item that is returned from the <TT><FONT FACE="Courier">each()</FONT></TT>
function call.
<P>
The code in line 30 is important and deserves some explaining.
First of all, the <TT><FONT FACE="Courier">while()</FONT></TT>
loop is used here. The format for a <TT><FONT FACE="Courier">while</FONT></TT>
loop is defined as this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">while( conditionIsTrue) {<BR>
codeInLOOP<BR>
}<BR>
<BR>
codeOutOfLOOP</FONT></TT>
</BLOCKQUOTE>
<P>
If the condition in the <TT><FONT FACE="Courier">while</FONT></TT>
loop is a nonzero number, a nonempty string, or a nonempty list,
the code in the area <TT><FONT FACE="Courier">codeInLOOP</FONT></TT>
is executed. Otherwise, the next statement outside the loop (that
is, after the curly brace) is executed.
<P>
Second, look at how the list (<TT><FONT FACE="Courier">$key</FONT></TT>,<TT><FONT FACE="Courier">$value</FONT></TT>)
is mapped onto the list returned by the <TT><FONT FACE="Courier">each()</FONT></TT>
function. The first item of the returned list is assigned to <TT><FONT FACE="Courier">$key</FONT></TT>,
the next item to<TT><FONT FACE="Courier"> $value</FONT></TT>.
This is part of the array-slicing operations available in Perl.
<H2><A NAME="ArrayOperations"><FONT SIZE=5 COLOR=#FF0000>Array
Operations</FONT></A></H2>
<P>
When working with arrays in Perl, you are really working with
lists. You can add or remove items from the front or back of the
list. Items in the middle of the list can be indexed using subscripts
or keys. Sublists can be created by extracting items from lists,
and lists can be concatenated to create one or more new lists.
<P>
Let's view some examples of how they fit together. See Listing
2.4, which uses some of these concepts.
<HR>
<BLOCKQUOTE>
<B>Listing 2.4. Array operations.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 #<BR>
3 # Array operations<BR>
4 #<BR>
5<BR>
6 $a = 'RFI';<BR>
7 $b = 'UPS';<BR>
8 $c = 'SPIKE';<BR>
9<BR>
10 @words = ('DC','AC','EMI','SURGE');<BR>
11<BR>
12 $count = @words; # Get the count<BR>
13<BR>
14 #<BR>
15 # Using the for operator on a list<BR>
16 #<BR>
17 print "\n \@words = ";<BR>
18 for $i (@words) {<BR>
19 print "[$i] ";<BR>
20 }<BR>
21<BR>
22 print "\n";<BR>
23 #<BR>
24 # Using the for loop for indexing<BR>
25 #<BR>
26 for ($i=0;$i<$count;$i++) {<BR>
27 print "\n Words[$i] : $words[$i];";
<BR>
28 }<BR>
29 #<BR>
30 # print 40 equal signs<BR>
31 #<BR>
32 print "\n";<BR>
33 print "=" x 40;<BR>
34 print "\n";<BR>
35 #<BR>
36 # Extracting items into scalars<BR>
37 #<BR>
38 ($x,$y) = @words;<BR>
39 print "x = $x, y = $y \n";<BR>
40 ($w,$x,$y,$z) = @words;<BR>
41 print "w = $x, x = $x, y = $y, z = $z\n";<BR>
42<BR>
43 ($anew[0], $anew[3], $anew[9], $anew[5]) = @words;<BR>
44<BR>
45 $temp = @anew;<BR>
46<BR>
47 #<BR>
48 # print 40 equal signs<BR>
49 #<BR>
50 print "=" x 40;<BR>
51 print "\n";<BR>
52<BR>
53 print "Number of elements in anew = ". $temp, "\n";
<BR>
54 print "Last index in anew = ". $#anew, "\n";
<BR>
55 print "The newly created Anew arrary is: ";<BR>
56 $j = 0;<BR>
57 for $i (@anew) {<BR>
58 print "\n \$anew[$j] = is $i ";
<BR>
59 $j++;<BR>
60 }<BR>
61 print "\n";<BR>
62<BR>
63</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here's the output from Listing 2.4:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> @words = [DC] [AC] [EMI] [SURGE]
<BR>
<BR>
Words[0] : DC;<BR>
Words[1] : AC;<BR>
Words[2] : EMI;<BR>
Words[3] : SURGE;<BR>
========================================<BR>
x = DC, y = AC<BR>
w = AC, x = AC, y = EMI z = SURGE<BR>
========================================<BR>
Number of elements in anew = 10<BR>
Last index in anew = 9<BR>
The newly created Anew arrary is:<BR>
$anew[0] = is DC<BR>
$anew[1] = is<BR>
$anew[2] = is<BR>
$anew[3] = is AC<BR>
$anew[4] = is<BR>
$anew[5] = is SURGE<BR>
$anew[6] = is<BR>
$anew[7] = is<BR>
$anew[8] = is<BR>
$anew[9] = is EMI</FONT></TT>
</BLOCKQUOTE>
<P>
Lines 6, 7, and 8 assign values to scalars <TT><FONT FACE="Courier">$a</FONT></TT>,
<TT><FONT FACE="Courier">$b</FONT></TT>, and <TT><FONT FACE="Courier">$c</FONT></TT>,
respectively. In line 10, four values are assigned to the <TT><FONT FACE="Courier">@words</FONT></TT>
array. At line 12, you get a count of the number of elements in
the array.
<P>
The <TT><FONT FACE="Courier">for()</FONT></TT> loop statement
is used to cycle through each element in the list. Perl takes
each item in the <TT><FONT FACE="Courier">@words</FONT></TT> array,
assigns it to <TT><FONT FACE="Courier">$i</FONT></TT>, and then
executes the statements in the block of code between the curly
braces. You could rewrite line 17 as the following and get the
same result:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">for $i ('DC','AC','EMI','SURGE') {</FONT></TT>
</BLOCKQUOTE>
<P>
In the example in Listing 2.4, the value of each item is printed
with square brackets around it. Line 22 simply prints a new line.
<P>
Now look at line 26, where the <TT><FONT FACE="Courier">for</FONT></TT>
loop is defined. The syntax in the <TT><FONT FACE="Courier">for</FONT></TT>
loop will be very familiar to C programmers:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">for (startingCondition; endingCondition;
at_end_of_every_loop) {<BR>
execute_statements_in_this_block;
<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
In line 26, <TT><FONT FACE="Courier">$i</FONT></TT> is set to
zero when the <TT><FONT FACE="Courier">for</FONT></TT> loop is
started. Before Perl executes the next statement within the block,
it checks to see whether <TT><FONT FACE="Courier">$i</FONT></TT>
is less than <TT><FONT FACE="Courier">$count</FONT></TT>. If <TT><FONT FACE="Courier">$i</FONT></TT>
is less than <TT><FONT FACE="Courier">$count</FONT></TT>, the
<TT><FONT FACE="Courier">print</FONT></TT> statement is executed.
If <TT><FONT FACE="Courier">$i</FONT></TT> is greater than or
equal to <TT><FONT FACE="Courier">$count</FONT></TT>, the next
statement following the ending curly brace is executed. After
executing the last statement in a <TT><FONT FACE="Courier">for</FONT></TT>
loop code block (see line 28), Perl increments the value of <TT><FONT FACE="Courier">$i</FONT></TT>
with the statement for the end of loop: <TT><FONT FACE="Courier">$i++</FONT></TT>.
So <TT><FONT FACE="Courier">$i</FONT></TT> is incremented. Perl
goes back to the top of the loop to test for the ending condition
to see what to do next.
<P>
In lines 32 through 34, an output-delimiting line is printed with
40 equal signs. The <TT><FONT FACE="Courier">x</FONT></TT> operator
in line 33 causes = to be repeated by the number following it.
Another way to print a somewhat fancier line would be to use the
following in lines 32 through 34:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">32 print "[\n";<BR>
33 print "-=" x 20;<BR>
34 print "]\n";</FONT></TT>
</BLOCKQUOTE>
<P>
Next, in line 38 the first two items in <TT><FONT FACE="Courier">@words</FONT></TT>
are assigned to variables <TT><FONT FACE="Courier">$x</FONT></TT>
and <TT><FONT FACE="Courier">$y</FONT></TT>, respectively. The
rest of the items in <TT><FONT FACE="Courier">@words</FONT></TT>
are not used. In line 40, four items from <TT><FONT FACE="Courier">@words</FONT></TT>
are assigned to four variables. The mapping of items from <TT><FONT F
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -