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

📄 ch4.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
is assigned 128 or 10000000. Then, the value in <TT>$firstVar</TT>

is shifted left by two places. So the new value is 00100000 or

32, which is assigned to <TT>$secondVar</TT>.

<P>

The rightmost bit of a value is lost when the bits are shifted

right. You can see this in the next example.

<P>

This example will divide by 8 using the <TT>&gt;&gt;</TT>

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 value of 129-a bit pattern of 10000001-to </I><TT><I>$firstVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT>

Every odd value has the rightmost bit set.<BR>

Shift the bits inside </I><TT><I>$firstVar</I></TT><I>

three places to the right and assign the new value to </I><TT><I>$secondVar</I></TT><I>.

<BR>

Print the </I><TT><I>$secondVar</I></TT><I>

variable.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

$firstVar = 129;

$secondVar = $firstVar &gt;&gt; 3;

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

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

16

</PRE>

</BLOCKQUOTE>

<P>

SiNCe the bit value of 16 is 00010000, you can tell that the rightmost

bit has disappeared.

<P>

Here's a quick example using the <TT>&lt;&lt;</TT>

operator. We'll multiply 128 by 8.

<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 value of 128 to the </I><TT><I>$firstVar</I></TT><I>

variable.<BR>

Shift the bits inside </I><TT><I>$firstVar</I></TT><I>

two places to the left and assign the new value to </I><TT><I>$secondVar</I></TT><I>.

<BR>

Print the </I><TT><I>$secondVar</I></TT><I>

variable.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

$firstVar = 128;

$secondVar = $firstVar &lt;&lt; 3;

print $secondVar;

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

1024

</PRE>

</BLOCKQUOTE>

<P>

The value of 1024 is beyond the bounds of the 8 bits that the

other examples used. This was done to show you that the number

of bits available for your use is not limited to one byte. You

are really limited by however many bytes Perl uses for one scalar

variable-probably 4. You'll need to read the Perl documentation

that came with the interpreter to determine how many bytes your

scalar variables use.

<H2><A NAME="TheNumericRelationalOperators"><FONT SIZE=5 COLOR=#FF0000>

The Numeric Relational Operators</FONT></A></H2>

<P>

The <I>numeric relational </I>operators, listed in Table 4.9,

are used to test the relationship between two operands. You can

see if one operand is equal to another, if one operand is greater

than another, or if one operator is less than another.<BR>

<p>

<CENTER>

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

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

It is important to realize that the equality operator is a pair of equal signs and not just one. Quite a few bugs are introduced into programs because people forget this rule and use a single equal sign when testing conditions.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

<CENTER><B>Table 4.9&nbsp;&nbsp;The Numeric 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 == op2</TT></CENTER>

</TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is equal to <TT>op2</TT>. For example, 6 == 6 is true.

</TD></TR>

<TR><TD WIDTH=153><CENTER><TT>op1 != op2</TT></CENTER>

</TD><TD WIDTH=437>This operator returns true if <TT>op1</TT> is not equal to <TT>op2</TT>. For example, 6 != 7 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, 6 &lt; 7 is true.

</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 or equal to <TT>op2</TT>. For example, 7 &lt;= 7 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, 6 &gt; 5 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 or equal to <TT>op2</TT>. For example, 7 &gt;= 7 is true.

</TD></TR>

<TR><TD WIDTH=153><CENTER><TT>op1 &lt;=&gt; 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 about

controlling program flow in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> &quot;Control Statements.&quot;

Therefore, I'll show only an example of the <TT>&lt;=&gt;</TT>

comparison operator here.

<H3><A NAME="ExampleUsingtheltgtOperator">

Example: Using the &lt;=&gt; Operator</A></H3>

<P>

The <I>number comparison </I>operator is used to quickly tell

the relationship between one operand and another. It is frequently

used 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 &lt;=&gt; operator called the spaceship operator because of the way that it looks.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<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>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 &lt;=&gt; $midVar, &quot;\n&quot;);

print($midVar &lt;=&gt; $midVar, &quot;\n&quot;);

print($hiVar  &lt;=&gt; $midVar, &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

-1

0

1

</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 can

see if one operand is equal to another, if one operand is greater

than another, or if one operator is less than another.<BR>

<P>

<CENTER><B>Table 4.10&nbsp;&nbsp;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, &quot;b&quot; eq &quot;b&quot; 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, &quot;b&quot; ne &quot;c&quot; 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, &quot;b&quot; lt &quot;c&quot; 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, &quot;b&quot; le &quot;b&quot; 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, &quot;b&quot; gt &quot;a&quot; 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, &quot;b&quot; ge &quot;b&quot; 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 character

in the strings. You will see examples of these operators when

you read about control program flow in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> &quot;Control

Statements.&quot; So, we'll only show an example of the <TT>cmp</TT>

comparison operator here. You may want to glaNCe at Appendix E,

&quot;ASCII Table,&quot; 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 &lt;<I>=</I>&gt;

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

$midVar = &quot;BBB&quot;;

$hiVar  = &quot;ccC&quot;;



print($lowVar cmp $midVar, &quot;\n&quot;);

print($midVar cmp $midVar, &quot;\n&quot;);

print($hiVar  cmp $midVar, &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

-1

0

1

</PRE>

</BLOCKQUOTE>

<P>

Notice that even though strings are being compared, a numeric

value is returned. You may be wondering what happens if the strings

have spaces in them. Let's explore that for a moment.

<BLOCKQUOTE>

<PRE>

$firstVar = &quot;AA&quot;;

$secondVar = &quot; A&quot;;

print($firstVar cmp $secondVar, &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

The program produces the following output:

<BLOCKQUOTE>

<PRE>

1

</PRE>

</BLOCKQUOTE>

<P>

which means that &quot;<TT>AA</TT>&quot;

is greater than &quot; <TT>A</TT>&quot;

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 operator

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

<P>

The value of the entire operation depends on the evaluation of

the CONDITION-PART section of the statement. If the CONDITION-PART

evaluates to true, then the TRUE-PART is the value of the entire

operation. If the CONDITION-PART evaluates to false, then the

FALSE-PART is the value of the entire operation.<BR>

<p>

<CENTER>

⌨️ 快捷键说明

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