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

📄 ch2.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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&nbsp;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 Perlhandles data. All computer programs use data in some way. Someuse it to personalize the program. For example, a mail programmight need to remember your name so that it can greet you uponstarting. Another program-say one that searches your hard diskfor files-might remember your last search parameters in case youwant to perform the same search twice.<P>A <I>literal</I> is a value that is represented &quot;as is&quot;or hard-coded in your source code. When you see the four characters45.5 in programs it really refers to a value of forty-five anda half. Perl uses four types of literals. Here is a quick glimpseat them:<UL><LI>Numbers-This is the most basic data type.<LI>Strings-A string is a series of characters that are handledas one unit.<LI>Arrays-An array is a series of numbers and strings handledas 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 lookupitem.</UL><P>Associative arrays will be discussed in <A HREF="ch3.htm" >Chapter 3</A> &quot;Variables.&quot;Numbers, strings, and regular arrays will be discussed in thefollowing sections.<H2><A NAME="NumericLiterals"><FONT SIZE=5 COLOR=#FF0000>Numeric Literals</FONT></A></H2><P>Numeric literals are frequently used. They represent a numberthat your program will need to work with. Most of the time youwill 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 mightalso 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 youcan use in your program code.<P>First, here are some integers.<P><IMG SRC="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>1230430x23</PRE></BLOCKQUOTE><P>Now, some numbers and fractions-also <I>called floating pointvalues</I>. You will frequently see these values referred to asa <I>float value</I> for simplicity's sake.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>A float with a value in the tenths place. You can also say100 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.554.534</PRE></BLOCKQUOTE><P>Here's a very small number.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>A very small float value. You can represent this value in scientificnotation 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 byquotes so that they can be used as a single datum. They are frequentlyused in programs to identify filenames, display messages, andprompt for input. In Perl you can use single quotes ('), doublequotes(&quot;), and back quotes (`).<H3><A NAME="ExampleSingleQuotedStrings">Example: Single-Quoted Strings</A></H3><P>The following examples show you how to use string literals. Stringliterals are widely used to identify filenames or when messagesare 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 textthat 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 &quot;Examples: Variable Interpolation&quot; in <A HREF="ch3.htm" >Chapter 3</A> &quot;Variables.&quot;</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="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 helpsWasWaldo 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 usea single quote inside the literal? If you did this, Perl wouldthink you wanted to end the string early and a compiler errorwould result. Perl uses the backslash (\) character to indicatethat the normal fuNCtion of the single quote-ending a literal-shouldbe 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" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>A literal that comments on WasWaldo's fighting ability. Noticehow the single quote is used.<BR>Another comment from the peanut gallery. Notice that double quotescan be used directly inside single-quoted strings.</I></BLOCKQUOTE><BLOCKQUOTE><PRE> 'WasWaldo can\'t hit the broad side of a barn.''Morganna said, &quot;WasWaldo can\'t hit anything.&quot;'</PRE></BLOCKQUOTE><P>The single-quotes are used here specifically so that the double-quotescan be used to surround the spoken words. Later in the sectionon double-quoted literals, you'll see that the single-quotes canbe replaced by double-quotes if you'd like.You must know onlyone more thing about single-quoted strings. You can add a linebreak to a single-quoted string simply by adding line breaks toyour source code-as demonstrated by Listing 2.1.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Tell Perl to begin printing.<BR>More Lines for Perl to display.<BR>The single quote ends the string literal.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 2.1&nbsp;&nbsp;02LST01.PL-Using Embedded Line Breaksto Skip to a New Line<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>print 'Bill of GoodsBread:    $34 .45Fruit:    $45.00          ======          $79.45';</PRE></BLOCKQUOTE><HR><P>Figure 2.1 shows a bill of goods displayed on one long, single-quotedliteral.<P><A HREF="f2-1.gif"><B>Figure 2.1 : </B><I>A bill of goods displayed one long single-quotedliteral</I>.</A><P>You can see that with single-quoted literals, even the line breaksin your source code are part of the string.<H3><A NAME="ExampleDoubleQuotedStrings">Example: Double-Quoted Strings</A></H3><P>Double-quoted strings start out simple, then become a bit moreinvolved than single-quoted strings. With double-quoted strings,you can use the backslash to add some special characters to yourstring. <A HREF="ch3.htm" >Chapter 3</A> &quot;Variables,&quot; will talk about howdouble-quoted strings and variables interact.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE><I>Variables</I>-which are described in <A HREF="ch3.htm" >Chapter 3</A> &quot;Variables&quot;-are simply locations in the computer's memory where Perl holds the various data types. They're called variables because the content of the memory can change as needed.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The basic double-quoted string is a series of characters surroundedby double quotes. If you need to use the double quote inside thestring, you can use the backslash character.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>This literal is similar to one you've already seen. Just thequotes are different.<BR>Another literal that uses double quotes inside a double-quotedstring.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>&quot;WasWaldo the Illusionist&quot;&quot;Morganna said, \&quot;WasWaldo can't hit anything.\&quot;&quot;</PRE></BLOCKQUOTE><P>Notice how the backslash in the second line is used to escapethe double quote characters. And the single quote can be usedwithout a backslash.<P>One major differeNCe between double- and single-quoted stringsis that double-quoted strings have some special <I>escape sequeNCes</I>that can be used. Escape sequeNCes represent characters that arenot easily entered using the keyboard or that are difficult tosee inside an editor window. Table 2.1 shows all of the escapesequeNCes that Perl understands. The examples following the tablewill illustrate some of them.<BR><P><CENTER><B>Table 2.1&nbsp;&nbsp;Escape SequeNCes</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=127><CENTER><I>Escape SequeNCes</I></CENTER></TD><TD WIDTH=463><I>Description or Character</I></TD></TR><TR><TD WIDTH=127><CENTER>\a</CENTER></TD><TD WIDTH=463>Alarm\bell</TD></TR><TR><TD WIDTH=127><CENTER>\b</CENTER></TD><TD WIDTH=463>Backspace</TD></TR><TR><TD WIDTH=127><CENTER>\e</CENTER></TD><TD WIDTH=463>Escape</TD></TR><TR><TD WIDTH=127><CENTER>\f</CENTER></TD><TD WIDTH=463>Form Feed</TD></TR><TR><TD WIDTH=127><CENTER>\n</CENTER></TD><TD WIDTH=463>Newline</TD></TR><TR><TD WIDTH=127><CENTER>\r</CENTER></TD><TD WIDTH=463>Carriage Return</TD></TR><TR><TD WIDTH=127><CENTER>\t</CENTER></TD><TD WIDTH=463>Tab</TD></TR><TR><TD WIDTH=127><CENTER>\v</CENTER></TD><TD WIDTH=463>Vertical Tab</TD></TR><TR><TD WIDTH=127><CENTER>\$</CENTER></TD><TD WIDTH=463>Dollar Sign</TD></TR><TR><TD WIDTH=127><CENTER>\@</CENTER></TD><TD WIDTH=463>Ampersand</TD></TR><TR><TD WIDTH=127><CENTER>\0nnn</CENTER></TD><TD WIDTH=463>Any Octal byte</TD></TR><TR><TD WIDTH=127><CENTER>\xnn</CENTER></TD><TD WIDTH=463>Any Hexadecimal byte</TD></TR><TR><TD WIDTH=127><CENTER>\cn</CENTER></TD><TD WIDTH=463>Any Control character</TD></TR><TR><TD WIDTH=127><CENTER>\l</CENTER></TD><TD WIDTH=463>Change the next character to lowercase</TD></TR><TR><TD WIDTH=127><CENTER>\u</CENTER></TD><TD WIDTH=463>Change the next character to uppercase</TD></TR><TR><TD WIDTH=127><CENTER>\L</CENTER></TD><TD WIDTH=463>Change the following characters to lowercase until a \E sequeNCe is eNCountered. Note that you need to use an uppercase E here, lowercase will not work.</TD></TR><TR><TD WIDTH=127><CENTER>\Q</CENTER></TD><TD WIDTH=463>Quote meta-characters as literals. See <A HREF="ch10.htm" >Chapter 10</A>, &quot;Regular Expressions,&quot; for more information on meta-characters.</TD></TR><TR><TD WIDTH=127><CENTER>\U</CENTER></TD><TD WIDTH=463>Change the following characters to uppercase until a \E sequeNCe is eNCountered. Note that you need to use an uppercase E here, lowercase will not work.</TD></TR><TR><TD WIDTH=127><CENTER>\E</CENTER></TD><TD WIDTH=463>Terminate the \L, \Q, or \U sequeNCe. Note that you need to use an uppercase E here, lowercase will not work.</TD></TR><TR><TD WIDTH=127><CENTER>\\</CENTER></TD><TD WIDTH=463>Backslash</TD></TR></TABLE></CENTER><P><p><CENTER>

⌨️ 快捷键说明

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