📄 ch2.htm
字号:
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x <= $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is less than or equal to <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x > $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is greater than <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x >= $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is greater than or equal to <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x eq $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is equal to string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x ne $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is not equal to string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x lt $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is less than string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x le $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is less than or equal to string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x gt $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is greater than string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x ge $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if string <TT><FONT FACE="Courier">$x</FONT></TT> is greater than or equal to string <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x x $y</FONT></TT>
</TD><TD WIDTH=416>Repeats <TT><FONT FACE="Courier">$x</FONT></TT>, <TT><FONT FACE="Courier">$y</FONT></TT> times
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x . $y</FONT></TT>
</TD><TD WIDTH=416>Returns the concatenated value of <TT><FONT FACE="Courier">$x</FONT></TT> and <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x cmp $y</FONT></TT>
</TD><TD WIDTH=416>Returns <TT><FONT FACE="Courier">1</FONT></TT> if <TT><FONT FACE="Courier">$x gt $y</FONT></TT>; <TT><FONT FACE="Courier">0</FONT></TT> if <TT><FONT FACE="Courier">$x eq $y</FONT></TT>; <TT><FONT FACE="Courier">-1</FONT></TT> if
<TT><FONT FACE="Courier">$x lt $y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$w ? $x : $y</FONT></TT>
</TD><TD WIDTH=416>Returns <TT><FONT FACE="Courier">$x</FONT></TT> if <TT><FONT FACE="Courier">$w</FONT></TT> is <TT><FONT FACE="Courier">true</FONT></TT>; <TT><FONT FACE="Courier">$y</FONT></TT> if <TT><FONT FACE="Courier">$w</FONT></TT> is <TT><FONT
FACE="Courier">false</FONT></TT>
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="ArraysandAssociativeArrays"><FONT SIZE=5 COLOR=#FF0000>Arrays
and Associative Arrays</FONT></A></H2>
<P>
Perl has arrays to let you group items using a single variable
name. Perl offers two types of arrays: those whose items are indexed
by number (arrays) and those whose items are indexed by a string
(associative arrays). An index into an array is referred to as
the <I>subscript</I> of the array.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
An associative array is referred to as "hash" because of the way it's stored internally in Perl.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Arrays are referred to with the <TT><FONT FACE="Courier">@</FONT></TT>
symbol. Individual items in an array are derived with a <TT><FONT FACE="Courier">$</FONT></TT>
and the subscript. Therefore, the first item in an array <TT><FONT FACE="Courier">@count</FONT></TT>
would be <TT><FONT FACE="Courier">$count[0]</FONT></TT>, the second
item would be <TT><FONT FACE="Courier">$count[1]</FONT></TT>,
and so on. See Listing 2.2 for usage of arrays.
<HR>
<BLOCKQUOTE>
<B>Listing 2.2. Using arrays.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 #<BR>
3 # An example to show how arrays work in Perl<BR>
4 #<BR>
5 @amounts = (10,24,39);<BR>
6 @parts = ('computer', 'rat', "kbd");<BR>
7<BR>
8 $a = 1; $b = 2; $c = '3';<BR>
9 @count = ($a, $b, $c);<BR>
10<BR>
11 @empty = ();<BR>
12<BR>
13 @spare = @parts;<BR>
14<BR>
15 print '@amounts = ';<BR>
16 print "@amounts \n";<BR>
17<BR>
18 print '@parts = ';<BR>
19 print "@parts \n";<BR>
20<BR>
21 print '@count = ';<BR>
22 print "@count \n";<BR>
23<BR>
24 print '@empty = ';<BR>
25 print "@empty \n";<BR>
26<BR>
27 print '@spare = ';<BR>
28 print "@spare \n";<BR>
29<BR>
30<BR>
31 #<BR>
32 # Accessing individual items in an array<BR>
33 #<BR>
34 print '$amounts[0] = ';<BR>
35 print "$amounts[0] \n";<BR>
36 print '$amounts[1] = ';<BR>
37 print "$amounts[1] \n";<BR>
38 print '$amounts[2] = ';<BR>
39 print "$amounts[2] \n";<BR>
40 print '$amounts[3] = ';<BR>
41 print "$amounts[3] \n";<BR>
42<BR>
43 print "Items in \@amounts = $#amounts \n";
<BR>
44 $size = @amounts; print "Size of Amount = $size\n";
<BR>
45 print "Item 0 in \@amounts = $amounts[$[]\n";<BR>
46</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here's the output from Listing 2.2:<BR>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">@amounts = 10 24 39<BR>
@parts = computer rat kbd<BR>
@count = 1 2 3<BR>
@empty =<BR>
@spare = computer rat kbd<BR>
$amounts[0] = 10<BR>
$amounts[1] = 24<BR>
$amounts[2] = 39<BR>
$amounts[3] =<BR>
Items in @amounts = 2<BR>
Size of Amount = 3<BR>
Item 0 in @amounts = 10</FONT></TT>
</BLOCKQUOTE>
<P>
In line 5, three integer values are assigned to the <TT><FONT FACE="Courier">@amounts</FONT></TT>
array. In line 6, three strings are assigned to the <TT><FONT FACE="Courier">@parts</FONT></TT>
array. In line 8, the script assigns both string and numeric values
to variables and then assigns the values of the variables to the
<TT><FONT FACE="Courier">@count</FONT></TT> array. An empty array
is created in line 11. In line 12, the <TT><FONT FACE="Courier">@spare</FONT></TT>
array is assigned the same values as those in <TT><FONT FACE="Courier">@parts</FONT></TT>.
<P>
Lines 15 through 28 print out the first five lines of the output.
In lines 34 to 41, the script addresses individual items of the
<TT><FONT FACE="Courier">@amounts</FONT></TT> array. Note that
<TT><FONT FACE="Courier">$amounts[3]</FONT></TT> does not exist;
therefore, it is printed as an empty item.
<P>
The <TT><FONT FACE="Courier">@#array</FONT></TT> syntax is used
in line 43 to print the last index in an array, so the script
prints <TT><FONT FACE="Courier">2</FONT></TT>. The size of the
amounts array is (<TT><FONT FACE="Courier">$#amounts</FONT></TT>
+ 1). If an array is assigned to a scalar, as shown in line 44,
the size of the array is assigned to the scalar.
<P>
Line 45 shows the use of a special Perl variable, <TT><FONT FACE="Courier">$[</FONT></TT>,
which is the base subscript (<TT><FONT FACE="Courier">0</FONT></TT>)
of an array.
<H3><A NAME="WhatAreAssociativeArrays">What Are Associative Arrays?</A>
</H3>
<P>
An associative array is really an array with two items per index.
The first item at each index is called a key and the other item
is called a value. You index into an associative array using keys
to get values. An associative array name is preceded with a percent
(<TT><FONT FACE="Courier">%</FONT></TT>) sign and indexed items
are enclosed within curly braces (<TT><FONT FACE="Courier">{}</FONT></TT>).
See Listing 2.3 for some sample uses of associative arrays.
<HR>
<BLOCKQUOTE>
<B>Listing 2.3. Using associative arrays.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 #<BR>
3 # Associative Arrays.<BR>
4 #<BR>
5<BR>
6 %subscripts = (<BR>
7 'bmp', 'Bitmap',<BR>
8 "cpp", "C++
Source",<BR>
9 "txt", 'Text
file' );<BR>
10<BR>
11 $bm = 'asc';<BR>
12 $subscripts{$bm} = 'Ascii File';<BR>
13<BR>
14 print "\n =========== Raw dump of hash =========
\n";<BR>
15 print %subscripts;<BR>
16<BR>
17 print "\n =========== using foreach =========
\n";<BR>
18 foreach $key (keys (%subscripts)) {<BR>
19 $value = $subscripts{$key};<BR>
20 print "Key = $key, Value = $value
\n";<BR>
21 }<BR>
22<BR>
23 print "\n === using foreach with sort ========= \n";
<BR>
24 foreach $key (sort keys (%subscripts)) {<BR>
25 $value = $subscripts{$key};<BR>
26 print "Key = $key, Value = $value
\n";<BR>
27 }<BR>
28<BR>
29 print "\n =========== using each() =========
\n";<BR>
30 while (($key,$value) = each(%subscripts)) {<BR>
31 print "Key = $key, Value = $value
\n";<BR>
32 }<BR>
33</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here's the output from Listing 2.3:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> =========== Raw dump of hash =========
<BR>
txtText filecppC++ SourceascAscii FilebmpBitmap<BR>
=========== using foreach =========<BR>
Key = txt, Value = Text file<BR>
Key = cpp, Value = C++ Source<BR>
Key = asc, Value = Ascii File<BR>
Key = bmp, Value = Bitmap<BR>
<BR>
=== using foreach with sort =========<BR>
Key = asc, Value = Ascii File<BR>
Key = bmp, Value = Bitmap<BR>
Key = cpp, Value = C++ Source<BR>
Key = txt, Value = Text file<BR>
<BR>
=========== using each() =========<BR>
Key = txt, Value = Text file<BR>
Key = cpp, Value = C++ Source<BR>
Key = asc, Value = Ascii File<BR>
Key = bmp, Value = Bitmap</FONT></TT>
</BLOCKQUOTE>
<P>
An associative array called <TT><FONT FACE="Courier">%subscripts</FONT></TT>
is created in line 6 to line 9. Three items of (<TT><FONT FACE="Courier">key</FONT></TT>,<TT><FONT FACE="Courier">value</FONT></TT>)
pairs are added to <TT><FONT FACE="Courier">%subscripts</FONT></TT>
as a list. At line 11, a new item is added to the <TT><FONT FACE="Courier">%subscript</FONT></TT>
array by assigning <TT><FONT FACE="Courier">$bm</FONT></TT> to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -