📄 ch4.htm
字号:
</TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is greater than or equal to <TT>op2</TT>. For example, 7 >= 7 is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 <=> op2</TT></CENTER></TD><TD WIDTH=437>This operator returns 1 if <TT>op1</TT> is greater than <TT>op2</TT>, 0 if <TT>op1</TT> equals <TT>op2</TT>, and -1 if <TT>op1</TT> is less than <TT>op2</TT>. </TD></TR></TABLE></CENTER><P><P>You will see many examples of these operators when you read aboutcontrolling program flow in <A HREF="ch7.htm" >Chapter 7</A> "Control Statements."Therefore, I'll show only an example of the <TT><=></TT>comparison operator here.<H3><A NAME="ExampleUsingtheltgtOperator">Example: Using the <=> Operator</A></H3><P>The <I>number comparison </I>operator is used to quickly tellthe relationship between one operand and another. It is frequentlyused during sorting activities.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>You may sometimes see the <=> operator called the spaceship operator because of the way that it looks.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Set up three variables.<BR>Print the relationship of each variable to the variable </I><TT><I>$midVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$lowVar = 8;$midVar = 10;$hiVar = 12;print($lowVar <=> $midVar, "\n");print($midVar <=> $midVar, "\n");print($hiVar <=> $midVar, "\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>-101</PRE></BLOCKQUOTE><P>The -1 indicates that <TT>$lowVar (8)</TT>is less than <TT>$midVar (10)</TT>.The 0 indicates that <TT>$midVar</TT>is equal to itself. And the 1 indicates that <TT>$hiVar(12)</TT> is greater than <TT>$midVar(10)</TT>.<H2><A NAME="TheStringRelationalOperators"><FONT SIZE=5 COLOR=#FF0000>The String Relational Operators</FONT></A></H2><P>The <I>string relational operators</I>, listed in Table 4.10,are used to test the relationship between two operands. You cansee if one operand is equal to another, if one operand is greaterthan another, or if one operator is less than another.<BR><P><CENTER><B>Table 4.10 The String Relational Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=153><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=437><I>Description</I></TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>The Equality Operators</B></TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 eq op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is equal to <TT>op2</TT>. For example, "b" eq "b" is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 ne op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is not equal to <TT>op2</TT>. For example, "b" ne "c" is true.</TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>The Comparison Operators</B></TD></TR><TR><TD WIDTH=153><CENTER><TT>op1 lt op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is less than <TT>op2</TT>. For example, "b" lt "c" is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 le op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is less than or equal to <TT>op2</TT>. For example, "b" le "b" is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 gt op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is greater than <TT>op2</TT>. For example, "b" gt "a" is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 ge op2</TT></CENTER></TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is greater than or equal to <TT>op2</TT>. For example, "b" ge "b" is true.</TD></TR><TR><TD WIDTH=153><CENTER><TT>Op1 cmp op2</TT></CENTER></TD><TD WIDTH=437>This operator returns 1 if <TT>op1</TT> is greater than <TT>op2</TT>, 0 if <TT>op1</TT> equals <TT>op2</TT>, and -1 if <TT>op1</TT> is less than <TT>op2</TT>. </TD></TR></TABLE></CENTER><P><P>String values are compared using the ASCII values of each characterin the strings. You will see examples of these operators whenyou read about control program flow in <A HREF="ch7.htm" >Chapter 7</A> "ControlStatements." So, we'll only show an example of the <TT>cmp</TT>comparison operator here. You may want to glaNCe at Appendix E,"ASCII Table," to see all of the possible ASCII values.<H3><A NAME="ExampleUsingthecmpOperator">Example: Using the cmp Operator</A></H3><P>The string comparison operator acts exactly like the <<I>=</I>>operator except that it is designed to work with string operands.This example will compare the values of three different strings.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Set up three variables.<BR><I>Print the relationship of each variable to the variable </I></I><TT><I>$midVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT></I></BLOCKQUOTE><BLOCKQUOTE><PRE>$lowVar = "AAA";$midVar = "BBB";$hiVar = "ccC";print($lowVar cmp $midVar, "\n");print($midVar cmp $midVar, "\n");print($hiVar cmp $midVar, "\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>-101</PRE></BLOCKQUOTE><P>Notice that even though strings are being compared, a numericvalue is returned. You may be wondering what happens if the stringshave spaces in them. Let's explore that for a moment.<BLOCKQUOTE><PRE>$firstVar = "AA";$secondVar = " A";print($firstVar cmp $secondVar, "\n");</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>1</PRE></BLOCKQUOTE><P>which means that "<TT>AA</TT>"is greater than " <TT>A</TT>"according to the criteria used by the <TT>cmp</TT>operator.<H2><A NAME="TheTernaryOperator"><FONT SIZE=5 COLOR=#FF0000>The Ternary Operator</FONT></A></H2><P>The <I>ternary</I> is actually a sequeNCe of operators. The operatoris used like this:<BLOCKQUOTE><PRE>CONDITION-PART ? TRUE-PART : FALSE-PART</PRE></BLOCKQUOTE><P>which is shorthand for the following statement:<BLOCKQUOTE><PRE>if (CONDITION-PART) { TRUE-PART} else { FALSE-PART}</PRE></BLOCKQUOTE><P>You can find more information about <TT>if</TT>statements in <A HREF="ch7.htm" >Chapter 7</A> "Control Statements."<P>The value of the entire operation depends on the evaluation ofthe CONDITION-PART section of the statement. If the CONDITION-PARTevaluates to true, then the TRUE-PART is the value of the entireoperation. If the CONDITION-PART evaluates to false, then theFALSE-PART is the value of the entire operation.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>The ternary operator is also referred to as the conditional operator by some refereNCes.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingtheTernaryOperatortoAssignValues">Example: Using the Ternary Operator to Assign Values</A></H3><P>I frequently use the ternary operator to assign a value to a variablewhen it can take one of two values. This use of the operator isfairly straightforward.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>If<FONT FACE="MCPdigital-BI"> </FONT></I><TT><I>$firstVar</I></TT><I>is zero, then assign </I><TT><I>$secondVar</I></TT><I>a value of zero. Otherwise, assign </I><TT><I>$secondVar</I></TT><I>the value in the first element in the array </I><TT><I>@array</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT></I></BLOCKQUOTE><BLOCKQUOTE><PRE>$secondVar = ($firstVar == 0) ? 0 : $array[0];</PRE></BLOCKQUOTE><P>The ternary operator can also be used to control which code sectionsare performed. However, I recommend against this use because itmakes the program harder to read. I believe that operators shouldaffect variables, not program flow.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>The CONDITION-PART evaluates to true so the </I><TT><I>$firstVar</I></TT><I>variable is iNCremented.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>1 ? $firstVar++ : $secondVar++;</PRE></BLOCKQUOTE><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>The CONDITION-PART evaluates to false so the </I><TT><I>$secondVar</I></TT><I>variable is iNCremented.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>0 ? $firstVar++ : $secondVar++;</PRE></BLOCKQUOTE><P>In this example, you get a chaNCe to see how the language canbe abused. When you have more than two actions to consider, youcan nest ternary operators inside each other. However, as youcan see the result is confusing code.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign one of four values to </I><TT><I>$firstVar</I></TT><I>depending on the value of </I><TT><I>$temp</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = $temp == 0 ? $numFiles++ : ($temp == 1 ? $numRecords++ : ($temp == 3 ? $numBytes++ : $numErrors++));<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Abusing the language in this manner will make your programs difficult to understand and maintain. You can use the <TT>if</TT> statement for better looking and more maintainable code. See <A HREF="ch7.htm" >Chapter 7</A> "Control Statements," for more information.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>If you'd like to see a really strange use of the ternary operator,take a look at this next example. It uses the ternary operatorto determine which variable gets assigned a value.<BLOCKQUOTE><PRE>$firstVar = 1;$secondVar = 1;$thirdVar = 1;($thirdVar == 0 ? $firstVar : $secondVar) = 10;print "$firstVar\n";print "$secondVar\n";print "$thirdVar\n";</PRE></BLOCKQUOTE><P>The program produces the following output:<BLOCKQUOTE><PRE>1101</PRE></BLOCKQUOTE><P>The line <TT>($thirdVar == 0 ? $firstVar: $secondVar) = 10;</TT> is equivalent to the followingcontrol statement:<BLOCKQUOTE><PRE>if ($thirdVar ==0) { $firstVar = 10;} else { $secondVar = 10;}</PRE></BLOCKQUOTE><P>This use of the ternary operator works because Perl lets you usethe results of evaluations as <I>lvalues</I>. An lvalue is anythingthat you can assign a value to. It's called an lvalue becauseit goes on the left side of an assignment operator.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Some programmers might think that this use of the ternary operator is as bad as using it to control program flow. However, I like this ability because it gives you the ability to coNCisely determine which variable is the target of an assignment. </BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheRangeOperator"><FONT SIZE=5 COLOR=#FF0000>The Range Operator (..)</FONT></A></H2><P>The range operator was already introduced to you in <A HREF="ch3.htm" >Chapter 3</A>"Variables," when you read about arrays. I review itsuse here-in an array context-in a bit more detail.<H3><A NAME="ExampleUsingtheRangeOperator">Example: Using the Range Operator</A></H3><P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -