📄 ch4.htm
字号:
When used with arrays, the range operator simplifies the processof creating arrays with contiguous sequeNCes of numbers and letters.We'll start with an array of the numbers one through ten.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array with ten elements that iNClude 1, 2, 3, 4,5, 6, 7, 8, 9, and 10.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = (1..10);</PRE></BLOCKQUOTE><P>You can also create an array of contiguous letters.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array with ten elements that iNClude A, B, C, D,E, F, G, H, I , and J.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ("A".."J");</PRE></BLOCKQUOTE><P>And, of course, you can have other things in the array definitionbesides the range operator.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array that iNCludes AAA, 1, 2, 3, 4, 5, A, B, C,D, and ZZZ.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ("AAA", 1..5, "A".."D", "ZZZ");</PRE></BLOCKQUOTE><P>You can use the range operator to create a list with zero-fillednumbers.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array with ten elements that iNClude the strings01, 02, 03, 04, 05, 06, 07, 08, 09, and 10.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ("01".."10");</PRE></BLOCKQUOTE><P>And you can use variables as operands for the range operator.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a string literal to </I><TT><I>$firstVar</I></TT><I><FONT FACE="MCPdigital-BI">.<BR></FONT>Create an array with ten elements that iNClude the strings01, 02, 03, 04, 05, 06, 07, 08, 09, and 10.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = "10";@array = ("01"..$firstVar);</PRE></BLOCKQUOTE><P>If you use strings of more than one character as operands, therange operator will iNCrement the rightmost character by one andperform the appropriate carry operation when the number 9 or letterz is reached. You'll probably need to see some examples beforethis makes sense. I know that I had trouble figuring it out. Sohere goes.<P>You've already seen "A".."Z," which is prettysimple to understand. Perl counts down the alphabet until Z isreached.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>The two ranges "A".."Z" and "a".."Z" are not identical. And the second range does not contain all lowercase letters and all uppercase letters. Instead, Perl creates an array that contains just the lowercase letters. Apparently, when Perl reaches the end of the alphabet-whether lowercase or uppercase-the iNCrementing stops.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>What happens when a two-character string is used as an operandfor the range operator? Let's find out.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array that iNCludes the strings aa, ab, ac, ad, ae,and af.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ("aa" .. "af");</PRE></BLOCKQUOTE><P>This behaves as you'd expect, iNCrementing along the alphabetuntil the f letter is reached. However, if you change the firstcharacter of one of the operands, watch what happens.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an array that iNCludes the strings ay, az, ba, bb, bc,bd, be, and bf.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ("ay" .. "bf");</PRE></BLOCKQUOTE><P>When the second character is iNCremented to z, then the firstcharacter is iNCremented to b and the second character is setto a.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>If the right side of the range operator is greater than the left side, an empty array is created.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheStringOperatorsandx"><FONT SIZE=5 COLOR=#FF0000>The String Operators (. and x)</FONT></A></H2><P>Perl has two different string operators-the coNCatenation (<TT>.</TT>)operator and the repetition (<TT>x</TT>)operator. These operators make it easy to manipulate strings incertain ways. Let's start with the coNCatenation operator.<H3><A NAME="ExampleUsingtheCoNCatenationOperator">Example: Using the CoNCatenation Operator</A></H3><P>The <I>coNCatenation</I> operator is used to join two stringstogether. If you have a numeric value as one of the two operands,Perl will quietly convert it to a string.<P>Here is an example that shows Perl converting a number into astring.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign a string value to </I><TT><I>$firstVar</I></TT><I>.The string will be three values coNCatenated into one string.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = "This box can hold " . 55 . " items.";print("$firstVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>This box can hold 55 items.</PRE></BLOCKQUOTE><P>The number 55 is automatically converted to a string and thencombined with the other strings. Notice that the string literalshave spaces in them so that when the final string is created,the number will be surrounded with spaces, making the senteNCereadable.<P>You can also use variables as operands with the coNCatenationoperator. <P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign string values to </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I>.<BR>Assign the coNCatenation of </I><TT><I>$firstVar</I></TT><I>and </I><TT><I>$secondVar</I></TT><I><FONT FACE="MCPdigital-BI"></FONT>to </I><TT><I>$thirdVar</I></TT><I><FONT FACE="MCPdigital-BI">.<BR></FONT>Print<FONT FACE="MCPdigital-BI"> </FONT></I><TT><I>$thirdVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT></I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = "AAA";$secondVar = "BBB";$thirdVar = $firstVar . $secondVar;print("$thirdVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output<BLOCKQUOTE><PRE>AAABBB</PRE></BLOCKQUOTE><P>Notice that Perl coNCatenates the strings together without addingany spaces or other separating characters. If you want a spacebetween the string after they are coNCatenated, you must ensurethat one of original strings has the space character-either atthe end of the first string or the start of the second.<H3><A NAME="ExampleUsingtheRepetitionOperator">Example: Using the Repetition Operator</A></H3><P>The <I>repetition</I> operator is used to repeat any string agiven number of times. Like the coNCatenation operator, any numberswill be quietly converted to strings so that they can be repeated.<P>Here is an example that shows how to repeat a string 7 times.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign </I><TT><I>$firstVar</I></TT><I>the value of "1".<BR>Assign </I><TT><I>$secondVar</I></TT><I>the value of </I><TT><I>$firstVar</I></TT><I>repeated seven times.<BR>Print </I><TT><I>$secondVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = "1";$secondVar = $firstVar x 7;print("$secondVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>1111111</PRE></BLOCKQUOTE><P>The string that gets repeated can be longer than one character.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign<FONT FACE="MCPdigital-BI"> </FONT></I><TT><I>$firstVar</I></TT><I>the value of "11 ".<BR>Assign </I><TT><I>$secondVar</I></TT><I>the value of </I><TT><I>$firstVar</I></TT><I>repeated seven times.<BR>Print </I><TT><I>$secondVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = "11 ";$secondVar = $firstVar x 7;print("$secondVar\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>11 11 11 11 11 11 11</PRE></BLOCKQUOTE><P>You can also use the repetition operator on arrays or lists. However,the array gets evaluated in a scalar context so that the numberof elements is returned. This number gets converted to a stringand then repeated.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign the elements "A" through "G" to</I><TT><I>@array</I></TT><I>.<BR>Get the number of elements in </I><TT><I>@array</I></TT><I>,convert that number to a string, repeat it twice, and then assignthe new string to </I><TT><I>$firstVar</I></TT><I>.<BR>Print the </I><TT><I>@array</I></TT><I>and </I><TT><I>$firstVar</I></TT><I>variables.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@array = ('A'..'G');$firstVar = @array x 2;print("@array\n");print("$firstVar\n");</PRE></BLOCKQUOTE><P>This program produces the following output:<BLOCKQUOTE><PRE>A B C D E F G77<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>If you want to repeat an array element, explicitly say which element you want to repeat, using an array index.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheAssignmentOperators"><FONT SIZE=5 COLOR=#FF0000>The Assignment Operators</FONT></A></H2><P>The last type of operators that we'll look at are <I>assignment</I>operators. You've already used the basic assignment operator (<I>=</I>)to value variables in some of the examples earlier in this chapter.In addition, Perl has shortcut assignment operators that combinethe basic assignment operator with another operator. For instaNCe,instead of saying <TT>$firstVar = $firstVar+ $secondVar</TT> you could say <TT>$firstVar+= $secondVar</TT>. The advantage of the using shortcutoperators-besides having less to type-is that your intentionsregarding assignment are made clear.<P>Table 4.11 lists all of Perl's assignment operators. After readingthe other sections in this chapter about the various operatortypes, you should be familiar with all of the operations describedin the table.<BR><P><CENTER><B>Table 4.11 The Assignment Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=139><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=432><I>Description</I></TD></TR><TR><TD WIDTH=139><CENTER><TT>var = op1;</TT></CENTER></TD><TD WIDTH=432>This operator assigns the value of <TT>op1</TT> to <TT>var</TT>.</TD></TR><TR><TD WIDTH=139><CENTER><TT>var += op1;</TT></CENTER></TD><TD WIDTH=432>This operator assigns the value of <TT>var + op1</TT> to <TT>var</TT>.</TD></TR><TR><TD WIDTH=139><CENTER><TT>var -= op1;</TT></CENTER></TD><TD WIDTH=432>This operator assigns the value of <TT>var - op1</TT> to <TT>var</TT>.</TD></TR><TR><TD WIDTH=139><CENTER><TT>var *= op1;</TT></CENTER></TD><TD WIDTH=432>This operator assigns the value of <TT>var
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -