⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch2.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</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 <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 &gt; $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 &gt;= $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 &quot;hash&quot; 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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 #<BR>

&nbsp;3 # An example to show how arrays work in Perl<BR>

&nbsp;4 #<BR>

&nbsp;5 @amounts = (10,24,39);<BR>

&nbsp;6 @parts = ('computer', 'rat', &quot;kbd&quot;);<BR>

&nbsp;7<BR>

&nbsp;8 $a = 1; $b = 2; $c = '3';<BR>

&nbsp;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 &quot;@amounts \n&quot;;<BR>

17<BR>

18 print '@parts = ';<BR>

19 print &quot;@parts \n&quot;;<BR>

20<BR>

21 print '@count = ';<BR>

22 print &quot;@count \n&quot;;<BR>

23<BR>

24 print '@empty = ';<BR>

25 print &quot;@empty \n&quot;;<BR>

26<BR>

27 print '@spare = ';<BR>

28 print &quot;@spare \n&quot;;<BR>

29<BR>

30<BR>

31 #<BR>

32 # Accessing individual items in an array<BR>

33 #<BR>

34 print '$amounts[0] = ';<BR>

35 print &quot;$amounts[0] \n&quot;;<BR>

36 print '$amounts[1] = ';<BR>

37 print &quot;$amounts[1] \n&quot;;<BR>

38 print '$amounts[2] = ';<BR>

39 print &quot;$amounts[2] \n&quot;;<BR>

40 print '$amounts[3] = ';<BR>

41 print &quot;$amounts[3] \n&quot;;<BR>

42<BR>

43 print &quot;Items in \@amounts&nbsp;&nbsp;= $#amounts \n&quot;;

<BR>

44 $size = @amounts; print &quot;Size of Amount&nbsp;&nbsp;= $size\n&quot;;

<BR>

45 print &quot;Item 0 in \@amounts = $amounts[$[]\n&quot;;<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&nbsp;&nbsp;= 2<BR>

Size of Amount&nbsp;&nbsp;= 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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 #<BR>

&nbsp;3 # Associative Arrays.<BR>

&nbsp;4 #<BR>

&nbsp;5<BR>

&nbsp;6 %subscripts = (<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'bmp', 'Bitmap',<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;cpp&quot;, &quot;C++

Source&quot;,<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;txt&quot;, 'Text

file' );<BR>

10<BR>

11 $bm = 'asc';<BR>

12 $subscripts{$bm} = 'Ascii File';<BR>

13<BR>

14 print &quot;\n =========== Raw dump of hash&nbsp;&nbsp;=========

\n&quot;;<BR>

15 print %subscripts;<BR>

16<BR>

17 print &quot;\n =========== using foreach&nbsp;&nbsp;=========

\n&quot;;<BR>

18 foreach $key (keys (%subscripts)) {<BR>

19&nbsp;&nbsp;&nbsp;&nbsp; $value = $subscripts{$key};<BR>

20&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Key = $key, Value = $value

\n&quot;;<BR>

21&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

22<BR>

23 print &quot;\n === using foreach with sort ========= \n&quot;;

<BR>

24 foreach $key (sort keys (%subscripts)) {<BR>

25&nbsp;&nbsp;&nbsp;&nbsp; $value = $subscripts{$key};<BR>

26&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Key = $key, Value = $value

\n&quot;;<BR>

27&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

28<BR>

29 print &quot;\n =========== using each()&nbsp;&nbsp;=========

\n&quot;;<BR>

30 while (($key,$value) = each(%subscripts)) {<BR>

31&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Key = $key, Value = $value

\n&quot;;<BR>

32&nbsp;&nbsp;&nbsp;&nbsp; }<BR>

33</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Here's the output from Listing 2.3:

<BLOCKQUOTE>

<TT><FONT FACE="Courier"> =========== Raw dump of hash&nbsp;&nbsp;=========

<BR>

txtText filecppC++ SourceascAscii FilebmpBitmap<BR>

 =========== using foreach&nbsp;&nbsp;=========<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()&nbsp;&nbsp;=========<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 + -