📄 ch4.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 4 -- Operators</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 4</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Operators</FONT></H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#OperatorTypes">
Operator Types</A>
<LI><A HREF="#TheBinaryArithmeticOperators">
The Binary Arithmetic Operators</A>
<UL>
<LI><A HREF="#ExampleTheExponentiationOperator">
Example: The Exponentiation Operator</A>
<LI><A HREF="#ExampleTheModulusOperator">
Example: The Modulus Operator</A>
</UL>
<LI><A HREF="#TheUnaryArithmeticOperators">
The Unary Arithmetic Operators</A>
<UL>
<LI><A HREF="#ExampleThePreiNCrementOperator">
Example: The Pre-iNCrement Operator</A>
<LI><A HREF="#ExampleThePredecrementOperator">
Example: The Pre-decrement Operator</A>
<LI><A HREF="#ExampleThePostiNCrementOperator">
Example: The Post-iNCrement Operator</A>
</UL>
<LI><A HREF="#TheLogicalOperators">
The Logical Operators</A>
<UL>
<LI><A HREF="#ExampleThequotANDquotOperatorampamp">
Example: The "AND" Operator (&&)</A>
<LI><A HREF="#ExampleThequotORquotOperator">
Example: The "OR" Operator (||)</A>
</UL>
<LI><A HREF="#ExampleThequotNOTquotOperator">
Example: The "NOT" Operator (!)</A>
<LI><A HREF="#TheBitwiseOperators">
The Bitwise Operators</A>
<UL>
<LI><A HREF="#ExampleUsingtheampandOperators">
Example: Using the &, |, and ^ Operators</A>
<LI><A HREF="#ExampleUsingthegtgtandltltOperators">
Example: Using the >> and << Operators</A>
</UL>
<LI><A HREF="#TheNumericRelationalOperators">
The Numeric Relational Operators</A>
<UL>
<LI><A HREF="#ExampleUsingtheltgtOperator">
Example: Using the <=> Operator</A>
</UL>
<LI><A HREF="#TheStringRelationalOperators">
The String Relational Operators</A>
<UL>
<LI><A HREF="#ExampleUsingthecmpOperator">
Example: Using the cmp Operator</A>
</UL>
<LI><A HREF="#TheTernaryOperator">
The Ternary Operator</A>
<UL>
<LI><A HREF="#ExampleUsingtheTernaryOperatortoAssignValues">
Example: Using the Ternary Operator to Assign Values</A>
</UL>
<LI><A HREF="#TheRangeOperator">
The Range Operator (..)</A>
<UL>
<LI><A HREF="#ExampleUsingtheRangeOperator">
Example: Using the Range Operator</A>
</UL>
<LI><A HREF="#TheStringOperatorsandx">
The String Operators (. and x)</A>
<UL>
<LI><A HREF="#ExampleUsingtheCoNCatenationOperator">
Example: Using the CoNCatenation Operator</A>
<LI><A HREF="#ExampleUsingtheRepetitionOperator">
Example: Using the Repetition Operator</A>
</UL>
<LI><A HREF="#TheAssignmentOperators">
The Assignment Operators</A>
<UL>
<LI><A HREF="#ExampleAssignmentUsingArraySlices">
Example: Assignment Using Array Slices</A>
<LI><A HREF="#ExampleAssigninganArraytoScalarVariables">
Example: Assigning an Array to Scalar Variables</A>
</UL>
<LI><A HREF="#OrderofPrecedeNCe">
Order of PrecedeNCe</A>
<UL>
<LI><A HREF="#ExampleOrderofPrecedeNCe">
Example: Order of PrecedeNCe</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
The <I>operators </I>in a computer language tell the computer
what actions to perform. Perl has more operators than most languages.
You've already seen some operators-like the equals or assignment
operator(=)-in this book. As you read about the other operators,
you'll undoubtedly realize that you are familiar with some of
them. Trust your intuition; the definitions that you already know
will probably still be true.
<P>
Operators are instructions you give to the computer so that it
can perform some task or operation. All operators cause actions
to be performed on <I>operands</I>. An operand can be anything
that you perform an operation on. In practical terms, any particular
operand will be a literal, a variable, or an expression. You've
already been introduced to literals and variables. A good working
definition of expression is some combination of operators and
operands that are evaluated as a unit. <A HREF="ch6.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch6.htm" >Chapter 6</A> "Statements,"
has more information about expressions.
<P>
Operands are also <I>recursive</I> in nature. In Perl, the expression
3 + 5-two operands and a plus operator-can be considered as one
operand with a value of 8. For instaNCe, (3 + 5) - 12 is an expression
that consists of two operands, the second of which is subtracted
from the first. The first operand is (3 + 5) and the second operand
is 12.
<P>
This chapter will discuss most of the operators available to you
in Perl . You'll find out about many operator types and how to
determine their order of precedeNCe. And, of course, you'll see
many examples.
<P>
PrecedeNCe is very important in every computer language and Perl
is no exception. The <I>order of precedeNCe</I> indicates which
operator should be evaluated first.
<P>
I like to think about operators in the same way I would give instructions
to the driver of a car. I might say "turn left" or "turn
right." These commands could be considered directional operators
in the same way that + and - say "add this" or "subtract
this." If I yell "stop" while the car is moving,
on the other hand, it should supersede the other commands. This
means that "stop" has precedeNCe over "turn left"
and "turn right." The "Order of PrecedeNCe"
section later in this chapter will discuss precedeNCe in more
detail.
<H2><A NAME="OperatorTypes"><FONT SIZE=5 COLOR=#FF0000>
Operator Types</FONT></A></H2>
<P>
Perl supports many types of operators. Table 4.1 shows all of
the operator types in the Perl language. This chapter discusses
the more commonly used types in detail. You can learn about any
type not discussed in this chapter by looking in the chapter refereNCed
in that type's description in Table 4.1.<BR>
<P>
<CENTER><B>Table 4.1 The Perl Operator Types</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=145><I>Operator Types</I></TD><TD WIDTH=445><I>Description</I>
</TD></TR>
<TR><TD WIDTH=145>Arithmetic</TD><TD WIDTH=445>These operators mirror those you learned in grade school. Addition, Subtraction, and Multiplication are the bread and butter of most mathematical statements.
</TD></TR>
<TR><TD WIDTH=145>Assignment</TD><TD WIDTH=445>These operators are used to assign a value to a variable. Algebra uses assignment operators. For example, in the statement X = 6, the equal sign is the assignment operator.
</TD></TR>
<TR><TD WIDTH=145>Binding</TD><TD WIDTH=445>These operators are used during string comparisons and are ex-plained in <A HREF="ch10.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch10.htm" >Chapter 10</A>, "Regular Expressions."
</TD></TR>
<TR><TD WIDTH=145>Bitwise</TD><TD WIDTH=445>These operators affect the individual bits that make up a value. For example, the value 3 is also 11 in binary notation or ((1 <FONT FACE="Symbol">¥</FONT> 2) + 1). Each character in binary notation
represents a <I>bit,</I> which is the smallest piece of a computer's memory that you can modify.
</TD></TR>
<TR><TD WIDTH=145>Comma</TD><TD WIDTH=445>The comma operator has two fuNCtions. It serves to separate array or list elements (see <A HREF="ch2.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch2.htm" >Chapter 2</A> "Numeric and String Literals") and it serves to separate expressions (see <A
HREF="ch6.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch6.htm" >Chapter 6</A> "Statements").
</TD></TR>
<TR><TD WIDTH=145>File Test</TD><TD WIDTH=445>These operators are used to test for various conditions associated with files. You can test for file existeNCe, file type, and file access rights among other things. See <A HREF="ch9.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch9.htm" >Chapter 9</A>
"Using Files," for more information.
</TD></TR>
<TR><TD WIDTH=145>List</TD><TD WIDTH=445>List operators are funny things in Perl. They resemble fuNCtion calls in other languages. List operators are discussed in <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A> "FuNCtions."
</TD></TR>
<TR><TD WIDTH=145>Logical</TD><TD WIDTH=445>These operators implement Boolean or true/false logic. In the senteNCe "If John has a fever AND John has clogged sinuses OR an earache AND John is NOT over 60 years old, then John has a cold," the AND,
OR, and NOT are acting as logical operators. The low precedeNCe logical operators will be discussed separately in <A HREF="ch13.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch13.htm" >Chapter 13</A>, "Handling Errors and Signals."
</TD></TR>
<TR><TD WIDTH=145>Numeric Relational</TD><TD WIDTH=445>These operators allow you to test the Relationalrelationship of one numeric variable to another. For example, is 5 GREATER THAN 12?
</TD></TR>
<TR><TD WIDTH=145>Postfix</TD><TD WIDTH=445>A member of this group of opera-tors-(), [], {}- appears at the end of the affected objects. You've already seen them used in <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A> "Vari-ables" for arrays and associative
arrays. The parentheses operators are also used for list operators as discussed in <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A> "FuNCtions."
</TD></TR>
<TR><TD WIDTH=145>Range</TD><TD WIDTH=445>The range operator is used to create a range of elements in arrays. It can also be used in a scalar context.
</TD></TR>
<TR><TD WIDTH=145>RefereNCe</TD><TD WIDTH=445>The refereNCe operators are used to manipulate variables. For more information, see <A HREF="ch8.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch8.htm" >Chapter 8</A> "Refer-eNCes."
</TD></TR>
<TR><TD WIDTH=145>String</TD><TD WIDTH=445>The string coNCatenation operator is used to join two strings together. The string repetition operator is used to repeat a string.
</TD></TR>
<TR><TD WIDTH=145>String Relational</TD><TD WIDTH=445>These operators allow you to test the relationship of one string variable to another. For example, is "abc" GREATER THAN "ABC"?
</TD></TR>
<TR><TD WIDTH=145>Ternary</TD><TD WIDTH=445>The ternary operator is used to choose between two choices based on a given condition. For instaNCe: If the park is within one mile, John can walk; otherwise, he must drive.
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="TheBinaryArithmeticOperators"><FONT SIZE=5 COLOR=#FF0000>
The Binary Arithmetic Operators</FONT></A></H2>
<P>
There are six <I>binary arithmetic operators</I>: addition, subtraction,
multiplication, exponentiation, division, and modulus. While you
may be unfamiliar with the modulus operator, the rest act exactly
as you would expect them to. Table 4.2 lists the arithmetic operators
that act on two operands-the binary arithmetic operators. In other
words, the addition (+) operator can be used to add two numbers
together like this: 4 + 5. The other binary operators act in a
similar fashion.<BR>
<P>
<CENTER><B>Table 4.2 The Binary Arithmetic Operators</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=151><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=132><I>Description</I>
</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 + op2</TT></CENTER>
</TD><TD WIDTH=132>Addition</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 - op2</TT></CENTER>
</TD><TD WIDTH=132>Subtraction</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 * op2</TT></CENTER>
</TD><TD WIDTH=132>Multiplication</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 ** op2</TT></CENTER>
</TD><TD WIDTH=132>Exponentiation</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 / op2</TT></CENTER>
</TD><TD WIDTH=132>Division</TD></TR>
<TR><TD WIDTH=151><CENTER><TT>op1 % op2</TT></CENTER>
</TD><TD WIDTH=132>Modulus</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="ExampleTheExponentiationOperator">
Example: The Exponentiation Operator</A></H3>
<P>
The <I>exponentiation</I> operator is used to raise a number to
a power. For instaNCe, 2 <FONT FACE="Symbol">**</FONT>4 is equivalent
to 2 <FONT FACE="Symbol">* </FONT> 2 <FONT FACE="Symbol">*</FONT>
2 <FONT FACE="Symbol">*</FONT> 2, which equals 16. You'll occasionally
see a refereNCe to when exponentiation discussion turns to how
efficient a given algorithm is, but I've never needed it for my
everyday programming tasks. In any case, here's a quick look at
how it works.
<P>
This example shows how to raise the number 4 to the 3<FONT SIZE=1>rd</FONT>
power, which is equivalent to 4 <FONT FACE="Symbol">*</FONT> 4
<FONT FACE="Symbol">*</FONT> 4 or 64.
<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 </I><TT><I>$firstVar</I></TT><I><FONT FACE="MCPdigital-BI">
</FONT>the value of 4.<BR>
Raise 4 to the 3<I><FONT SIZE=1>rd</FONT> power using the exponentiation
operator and assign the new value to </I></I><TT><I>$secondVar</I></TT><I><FONT FACE="MCPdigital-BI">.
<BR>
</FONT>Print </I><TT><I>$secondVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 4;
$secondVar = $firstVar ** 3;
print("$secondVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program produces the following output:
<BLOCKQUOTE>
<PRE>
64
</PRE>
</BLOCKQUOTE>
<H3><A NAME="ExampleTheModulusOperator">
Example: The Modulus Operator</A></H3>
<P>
The <I>modulus </I>operator is used to find the remainder of the
division between two integer operands. For instaNCe, 10 % 7 equals
3 because 10 / 7 equals 1 with 3 left over.
<P>
I've found the modulus operator to be useful when my programs
need to run down a list and do something every few items. This
example shows you how to do something every 10 items.
<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>Start a loop that begins with<FONT FACE="MCPdigital-BI"> </FONT></I><TT><I>$index</I></TT><I><FONT FACE="MCPdigital-BI">
</FONT>equal to zero.<BR>
If the value of </I><TT><I>$index</I></TT><I><FONT FACE="MCPdigital-BI">
% </FONT></I><TT><I>10</I></TT><I>
is equal to zero then the </I><TT><I>print</I></TT><I>
statement will be executed.<BR>
Print the value of </I><TT><I>$index</I></TT><I>
followed by space.<BR>
The program will iNCrease the value of </I><TT><I>$index</I></TT><I>
by one and then loop back to the start of the </I><TT><I>if</I></TT><I>
statement.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 4.1 O4LST01.PL-How to Display a Message
Every Ten Items<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
for ($index = 0; $index <= 100; $index++) {
if ($index % 10 == 0) {
print("$index ");
}
}
</PRE>
</BLOCKQUOTE>
<HR>
<P>
When this program is run, the output should look like the following:
<BLOCKQUOTE>
<PRE>
0 10 20 30 40 50 60 70 80 90 100
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -