📄 ch2.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 2 -- Numeric and String Literals</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 2</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Numeric and String Literals</FONT>
</H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#NumericLiterals">
Numeric Literals</A>
<UL>
<LI><A HREF="#ExampleNumbers">
Example: Numbers</A>
</UL>
<LI><A HREF="#StringLiterals">
String Literals</A>
<UL>
<LI><A HREF="#ExampleSingleQuotedStrings">
Example: Single-Quoted Strings</A>
<LI><A HREF="#ExampleDoubleQuotedStrings">
Example: Double-Quoted Strings</A>
<LI><A HREF="#ExampleBackQuotedStrings">
Example: Back-Quoted Strings</A>
</UL>
<LI><A HREF="#ArrayLiterals">
Array Literals</A>
<UL>
<LI><A HREF="#ExamplePrintinganArray">
Example: Printing an Array</A>
<LI><A HREF="#ExampleNestingArrays">
Example: Nesting Arrays</A>
<LI><A HREF="#ExampleUsingaRangeofValues">
Example: Using a Range of Values</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>
In this chapter, we'll take a look at some of the ways that Perl
handles data. All computer programs use data in some way. Some
use it to personalize the program. For example, a mail program
might need to remember your name so that it can greet you upon
starting. Another program-say one that searches your hard disk
for files-might remember your last search parameters in case you
want to perform the same search twice.
<P>
A <I>literal</I> is a value that is represented "as is"
or hard-coded in your source code. When you see the four characters
45.5 in programs it really refers to a value of forty-five and
a half. Perl uses four types of literals. Here is a quick glimpse
at them:
<UL>
<LI>Numbers-This is the most basic data type.
<LI>Strings-A string is a series of characters that are handled
as one unit.
<LI>Arrays-An array is a series of numbers and strings handled
as a unit. You can also think of an array as a list.
<LI>Associative Arrays-This is the most complicated data type.
Think of it as a list in which every value has an associated lookup
item.
</UL>
<P>
Associative arrays will be discussed in <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A> "Variables."
Numbers, strings, and regular arrays will be discussed in the
following sections.
<H2><A NAME="NumericLiterals"><FONT SIZE=5 COLOR=#FF0000>
Numeric Literals</FONT></A></H2>
<P>
Numeric literals are frequently used. They represent a number
that your program will need to work with. Most of the time you
will use numbers in base ten-the base that everyone uses. However,
Perl will also let you use base 8 (octal) or base 16 (hexadecimal).
<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
For those of you who are not familiar with non-decimal numbering systems, here is a short explanation.</BLOCKQUOTE>
<BLOCKQUOTE>
In decimal notation-or base ten- when you see the value 15 it signifies (1 * 10) + 5 or 15<FONT SIZE=1>10</FONT>. The subscript indicates which base is being used.
</BLOCKQUOTE>
<BLOCKQUOTE>
In octal notation-or base eight-when you see the value 15 it signifies (1 * 8) + 5 or 13<FONT SIZE=1>10</FONT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
In hexadecimal notation-or base 16-when you see the value 15 it signifies (1 * 16) + 5 or 21<FONT SIZE=1>10</FONT>. Base 16 needs an extra six characters in addition to 0 to 9 so that each position can have a total of 16 values. The letters A-F are used
to represent 11-16. So the value BD<FONT SIZE=1>16</FONT> is equal to (B<FONT SIZE=1>16</FONT> * 16) + D<FONT SIZE=1>16</FONT> or (11<FONT SIZE=1>10</FONT> * 16) + 13<FONT SIZE=1>10</FONT> which is 176<FONT SIZE=1>10</FONT>.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
If you will be using very large or very small numbers, you might
also find scientific notation to be of use.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you're like me, you probably forgot most of the math you learned in high school. However, scientific notation has always stuck with me. Perhaps because I liked moving decimal points around. Scientific notation looks like 10.23E+4, which is equivalent
to 102,300. You can also represent small numbers if you use a negative sign. For example, 10.23E-4 is .001023. Simply move the decimal point to the right if the exponent is positive and to the left if the exponent is negative.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="ExampleNumbers">
Example: Numbers</A></H3>
<P>
Let's take a look at some different types of numbers that you
can use in your program code.
<P>
First, here are some integers.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>
<BLOCKQUOTE>
<I>An integer. Integers are numbers with no decimal components.
<BR>
<I>An integer in octal format. This number is 35, or (4 *
8) + 3, in base 10.<BR>
An integer in hexadecimal format. This number is also 35, or (2
* 16) + 3 in base 10.</I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
123
043
0x23
</PRE>
</BLOCKQUOTE>
<P>
Now, some numbers and fractions-also <I>called floating point
values</I>. You will frequently see these values referred to as
a <I>float value</I> for simplicity's sake.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>
<BLOCKQUOTE>
<I>A float with a value in the tenths place. You can also say
100 and <FONT SIZE=1>5</FONT>/<FONT SIZE=1>10</FONT>.<BR>
<I>A float with a fraction value out to the thousandths place.
You can also say 54 and <FONT SIZE=1>534</FONT>/<FONT SIZE=1>1000.</FONT></I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
100.5
54.534
</PRE>
</BLOCKQUOTE>
<P>
Here's a very small number.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>
<BLOCKQUOTE>
<I>A very small float value. You can represent this value in scientific
notation as 3.4E-5.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
.000034
</PRE>
</BLOCKQUOTE>
<H2><A NAME="StringLiterals"><FONT SIZE=5 COLOR=#FF0000>
String Literals</FONT></A></H2>
<P>
<I>String Literals</I> are groups of characters surrounded by
quotes so that they can be used as a single datum. They are frequently
used in programs to identify filenames, display messages, and
prompt for input. In Perl you can use single quotes ('), double
quotes("), and back quotes (`).
<H3><A NAME="ExampleSingleQuotedStrings">
Example: Single-Quoted Strings</A></H3>
<P>
The following examples show you how to use string literals. String
literals are widely used to identify filenames or when messages
are displayed to users. First, we'll look at single-quoted strings,
then double-quoted strings.
<P>
A single-quoted string is pretty simple. Just surround the text
that you'd like to use with single quotes.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The real value of single-quoted strings won't become apparent until you read about variable interpolation in the section "Examples: Variable Interpolation" in <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A> "Variables."</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>
<BLOCKQUOTE>
<I>A literal that describes one of my favorite role-playing characters.
<BR>
A literal that describes the blessed cleric that frequently helps
WasWaldo stay alive.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
'WasWaldo the Illusionist'
'Morganna the Fair'
</PRE>
</BLOCKQUOTE>
<P>
Strings are pretty simple, huh? But what if you wanted to use
a single quote inside the literal? If you did this, Perl would
think you wanted to end the string early and a compiler error
would result. Perl uses the backslash (\) character to indicate
that the normal fuNCtion of the single quote-ending a literal-should
be ignored for a moment.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The backslash character is also called <I>an escape character</I>-perhaps because it lets the next character escape from its normal interpretation
</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>
<BLOCKQUOTE>
<I>A literal that comments on WasWaldo's fighting ability. Notice
how the single quote is used.<BR>
Another comment from the peanut gallery. Notice that double quotes
can be used directly inside single-quoted strings.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
'WasWaldo can\'t hit the broad side of a barn.'
'Morganna said, "WasWaldo can\'t hit anything."'
</PRE>
</BLOCKQUOTE>
<P>
The single-quotes are used here specifically so that the double-quotes
can be used to surround the spoken words. Later in the section
on double-quoted literals, you'll see that the single-quotes can
be replaced by double-quotes if you'd like.You must know only
one more thing about single-quoted strings. You can add a line
break to a single-quoted string simply by adding line breaks to
your source code-as demonstrated by Listing 2.1.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>
<BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -