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

📄 ch4.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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 variable

when it can take one of two values. This use of the operator is

fairly straightforward.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 sections

are performed. However, I recommend against this use because it

makes the program harder to read. I believe that operators should

affect variables, not program flow.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 can

be abused. When you have more than two actions to consider, you

can nest ternary operators inside each other. However, as you

can see the result is confusing code.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> &quot;Control Statements,&quot; 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 operator

to determine which variable gets assigned a value.

<BLOCKQUOTE>

<PRE>

$firstVar = 1;

$secondVar = 1;



$thirdVar = 1;



($thirdVar == 0 ? $firstVar : $secondVar) = 10;



print &quot;$firstVar\n&quot;;

print &quot;$secondVar\n&quot;;

print &quot;$thirdVar\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

1

10

1

</PRE>

</BLOCKQUOTE>

<P>

The line <TT>($thirdVar == 0 ? $firstVar

: $secondVar) = 10;</TT> is equivalent to the following

control 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 use

the results of evaluations as <I>lvalues</I>. An lvalue is anything

that you can assign a value to. It's called an lvalue because

it 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A>

&quot;Variables,&quot; when you read about arrays. I review its

use here-in an array context-in a bit more detail.

<H3><A NAME="ExampleUsingtheRangeOperator">

Example: Using the Range Operator</A></H3>

<P>

When used with arrays, the range operator simplifies the process

of 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 = (&quot;A&quot;..&quot;J&quot;);

</PRE>

</BLOCKQUOTE>

<P>

And, of course, you can have other things in the array definition

besides the range operator.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 = (&quot;AAA&quot;, 1..5, &quot;A&quot;..&quot;D&quot;, &quot;ZZZ&quot;);

</PRE>

</BLOCKQUOTE>

<P>

You can use the range operator to create a list with zero-filled

numbers.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Create an array with ten elements that iNClude the strings

01, 02, 03, 04, 05, 06, 07, 08, 09, and 10.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@array = (&quot;01&quot;..&quot;10&quot;);

</PRE>

</BLOCKQUOTE>

<P>

And you can use variables as operands for the range operator.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 strings

01, 02, 03, 04, 05, 06, 07, 08, 09, and 10.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

$firstVar = &quot;10&quot;;

@array = (&quot;01&quot;..$firstVar);

</PRE>

</BLOCKQUOTE>

<P>

If you use strings of more than one character as operands, the

range operator will iNCrement the rightmost character by one and

perform the appropriate carry operation when the number 9 or letter

z is reached. You'll probably need to see some examples before

this makes sense. I know that I had trouble figuring it out. So

here goes.

<P>

You've already seen &quot;A&quot;..&quot;Z,&quot; which is pretty

simple to understand. Perl counts down the alphabet until Z is

reached.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Caution</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The two ranges &quot;A&quot;..&quot;Z&quot; and &quot;a&quot;..&quot;Z&quot; 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 operand

for the range operator? Let's find out.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 = (&quot;aa&quot; .. &quot;af&quot;);

</PRE>

</BLOCKQUOTE>

<P>

This behaves as you'd expect, iNCrementing along the alphabet

until the f letter is reached. However, if you change the first

character of one of the operands, watch what happens.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 = (&quot;ay&quot; .. &quot;bf&quot;);

</PRE>

</BLOCKQUOTE>

<P>

When the second character is iNCremented to z, then the first

character is iNCremented to b and the second character is set

to 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 in

certain 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 strings

together. 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 a

string.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Assign a

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -