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

📄 ch2.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>In the next chapter, &quot;Variables,&quot; you'll see why you might need to use a backslash when using the $ and @ characters.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>This literal represents the following: WasWaldo is 34 yearsold. The \u is used twice in the first word to capitalize thew characters. And the hexadecimal notation is used to representthe age using the ASCII codes for 3 and 4.<BR>This literal represents the following: The kettle was HOT!. The\U capital-izes all characters until a \E sequeNCe is seen.</I></BLOCKQUOTE><BLOCKQUOTE><PRE> &quot;\uwas\uwaldo is \x33\x34 years old.&quot;&quot;The kettle was \Uhot\E!&quot;</PRE></BLOCKQUOTE><P>For more information about ASCII codes, see Appendix E, &quot;ASCIITable.&quot; <P>Actually, this example isn't too difficult, but it does involvelooking at more than one literal at oNCe and it's been a few pagessiNCe our last advaNCed example. Let's look at the \t and \n escapesequeNCes. Listing 2.2-a program displaying a bill with severalitems-will produce the output shown in Figure 2.2.<P><A HREF="f2-2.gif"><B>Figure 2.2 : </B><I>A bill of goods displayed using newlineand tab characters</I>.</A><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Display a literal as the first line, second and third of theoutput.<BR>Display literals that show what was purchased<BR>Display a separator line.<BR>Display the total.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 2.2&nbsp;&nbsp;02LST02.PL-Using Tabs and Newline Charactersto Print<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>print &quot;Bill of GoodsBread:\t\$34.45\n&quot;;print &quot;Fruit:\t&quot;;print &quot;\$45.00\n&quot;;print &quot;\t======\n&quot;;print &quot;\t\$79.45\n&quot;;</PRE></BLOCKQUOTE><HR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Notice that Figure 2.1 and 2.2 look identical. This illustrates a cardinal rule of Perl-there's <I>always</I> more than one way to do something.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>This program uses two methods to cause a line break.<UL><LI>The first is simply to iNClude the line break in the sourcecode.<LI>The second is to use the \n or newline character.</UL><P>I recommend using the \n character so that when looking at yourcode in the future, you can be assured that you meant to causea line break and did not simply press the ENTER key by mistake.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>If you are a C/C++ programmer, this material is not new to you. However, Perl strings are <I>not identical</I> to C/C++ strings because they have no ending NULL character. If you are thinking of converting C/C++ programs to Perl, take care to modify any code that relies on the NULL character to end a string.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleBackQuotedStrings">Example: Back-Quoted Strings</A></H3><P>It might be argued that back-quoted strings are not really a datatype. That's because Perl uses back-quoted strings to executesystem commands. When Perl sees a back-quoted string, it passesthe contents to Windows, UNIX, or whatever operating system youare using.<P>Let's see how to use the back-quoted string to display a directorylisting of all text files in the perl5 directory. <P>Figure 2.3 shows what the output of such a program might looklike.<P><A HREF="f2-3.gif"><B>Figure 2.3 : </B><I>Using a back-quoted string to display adirectory</I>.</A><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Print the directory listing.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>print &quot;dir *.txt&quot;;</PRE></BLOCKQUOTE><P>All of the escape sequeNCes used with double-quoted strings canbe used with back-quoted strings.<H2><A NAME="ArrayLiterals"><FONT SIZE=5 COLOR=#FF0000>Array Literals</FONT></A></H2><P>Perl uses <I>arrays</I>-or lists-to store a series of items. Youcould use an array to hold all of the lines in a file, to helpsort a list of addresses, or to store a variety of items. We'lllook at some simple arrays in this section. In the next chapter,&quot;Variables,&quot; you'll see more examples of how usefularrays can be.<H3><A NAME="ExamplePrintinganArray">Example: Printing an Array</A></H3><P>In this section, we'll look at printing an array and see how arraysare represented in Perl source code.<P>This example shows an empty array, an array of numbers and anarray of strings. <P>Figure 2.4 shows the output of Listing 2.3.<P><A HREF="f2-4.gif"><B>Figure 2.4 : </B><I>The output from Listing 2.3, showing differentarray literals</I>.</A><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Print the contents of an empty array.<BR><I>Print the contents of an array of numbers.<BR><I>Print the contents of an array of strings.<BR><I>Print the contents of an array with different data types.</I></I></I></I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 2.3&nbsp;&nbsp;02LST03.PL-Printing Some Array Literals<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>print &quot;Here is an empty array:&quot; . () . &quot;&lt;-- Nothing there!\n&quot;;print (12, 014, 0x0c, 34.34, 23.3E-3);print &quot;\n&quot;;print (&quot;This&quot;, &quot;is&quot;, 'an', &quot;array&quot;, 'of', &quot;strings&quot;);print &quot;\n&quot;;print (&quot;This&quot;, 30, &quot;is&quot;, 'a', &quot;mixed array&quot;, 'of', 0x08, &quot;items&quot;);.</PRE></BLOCKQUOTE><HR><P>The fourth line of this listing shows that you can mix single-and double-quoted strings in the same array. You can also mixnumbers and strings interchangeably, as shown in the last line.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Listing 2.3 uses the period, or <I>coNCatenation</I>, operator to join a string representation of the empty array with the string <TT>&quot;Here is an empty array:&quot;</TT> and the string <TT>&quot;&lt;-- Nothing there!\n&quot;</TT>. You can read more about operators in <A HREF="ch4.htm" >Chapter 4</A> &quot;Operators.&quot;</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>In this and other examples in this chapters, the elements of an array will be printed with no spaces between them. You will see how to print with spaces in the section &quot;Strings Revisited&quot; in <A HREF="ch3.htm" >Chapter 3</A> &quot;Variables.&quot;</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleNestingArrays">Example: Nesting Arrays</A></H3><P>Many times a simple list is not enough. If you're a painter, youmight have one array that holds the names of orange hues and onethat holds the names of yellow hues. To print them, you can usePerl's ability to specify a sub-array inside your main array definition.<P>While this example is not very &quot;real-world,&quot; it givesyou the idea behind specifying an array by using sub-arrays.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Print an array that consists of two sub-arrays.<BR><I>Print an array that consists of an array, a string, and anotherarray.</I></I></BLOCKQUOTE><BLOCKQUOTE><PRE>print ((&quot;Bright Orange&quot;, &quot;Burnt&quot;), (&quot;Canary Yellow&quot;, &quot;Sunbeam&quot;));print ((&quot;Bright Orange&quot;, &quot;Burnt&quot;), &quot; Middle &quot;, (&quot;Canary Yellow&quot;, &nbsp;&quot;Sunbeam&quot;));</PRE></BLOCKQUOTE><P>So far, we haven't talked about the internal representations ofdata types. That's because you almost never have to worry aboutsuch things with Perl. However, it is important to know that,internally, the sub-arrays are merged into the main array. Inother words, the <TT>array:</TT><BLOCKQUOTE><PRE>((&quot;Bright Orange&quot;, &quot;Burnt&quot;), (&quot;Canary Yellow&quot;, &quot;Sunbeam&quot;))</PRE></BLOCKQUOTE><P>is exactly equivalent to<BLOCKQUOTE><PRE>(&quot;Bright Orange&quot;, &quot;Burnt&quot;, &quot;Canary Yellow&quot;, &quot;Sunbeam&quot;)</PRE></BLOCKQUOTE><H3><A NAME="ExampleUsingaRangeofValues">Example: Using a Range of Values</A></H3><P>At times you might need an array that consists of sequential numbersor letters. Instead of making you list out the entire array, Perlhas a shorthand notation that you can use.<P>Perl uses two periods (..) to replace a consecutive series ofvalues. Not only is this method quicker to type-and less proneto error-it is easier to understand. Only the end points of theseries are specified; you don't need to manually verify that everyvalue is represented. If the .. is used, then automatically youknow that a range of values will be used.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Print an array consisting of the numbers from 1 to 15.<BR><I>Print an array consisting of the numbers from 1 to 15 usingthe shorthand method.</I></I></BLOCKQUOTE><BLOCKQUOTE><PRE>print (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);print &quot;\n&quot;;print (1..15);</PRE></BLOCKQUOTE><P>The two arrays used in the previous example are identical, butthey were specified differently.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The double periods in the array specification are called the <I>range </I>operator. The range operator is also discussed in <A HREF="ch4.htm" >Chapter 4</A> &quot;Operators.&quot;</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>You can also use the shorthand method to specify values in themiddle of an array.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><BLOCKQUOTE><I>Print an array consisting of the numbers 1, 2, 7, 8, 9, 10,14, and 15.Print an array consisting of the letters A, B, F, G,H, Y, Z</I></BLOCKQUOTE><BLOCKQUOTE><PRE>print (1, 2, 7..10, 14, 15);print &quot;\n&quot;print (&quot;A&quot;, &quot;B&quot;, &quot;F&quot;..&quot;H&quot;, &quot;Y&quot;, &quot;Z&quot;);</PRE></BLOCKQUOTE><P>The range operator works by taking the lefthand value, addingone to it, then appending that new value to the array. Perl continuesto do this until the new value reaches the righthand value. Youcan use letters with the range operator because the ASCII tableuses consecutive values to represent consecutive letters.<P>For more information about ASCII codes, see Appendix E, &quot;ASCIITable.&quot;<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter introduced you to both numeric and string literals.You learned that literals are values that are placed directlyinto your source code and never changed by the program. They aresometimes referred to as hard-coded values.<P>You read about numbers and the three different bases that canbe used to represent them-decimal, octal, and hexadecimal. Verylarge or small numbers can also be described using scientificnotation.<P>Strings were perhaps a bit more involved. Single-, double-, andback-quoted strings are used to hold strings of characters. Back-quotedstrings have an additional purpose. They tell Perl to send thestring to the operating system for execution.<P>Escape sequeNCes are used to represent characters that are difficultto enter through the keyboard or that have more than one purpose.For example, using a double quote inside a double-quoted stringwould end the string before you really intended. The backslashcharacter was introduced to escape the double quote and changeits meaning.<P>The next chapter, &quot;Variables,&quot; will show you how Perluses your computer memory to store data types and also will showyou ways that you can manipulate data.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>Review Questions</FONT></A></H2><P>Answers to Review Questions are in Appendix A.<OL><LI>What are the four types of literals?<LI>What is a numeric literal?<LI>How many types of string literals are there?<LI>What is the major differeNCe between single- and double-quotedstrings?<LI>What are three escape sequeNCes and what do they mean?<LI>What would the following one-line program display?<BR><BR><TT>print 'dir /*.log';<BR></TT><LI>What is scientific notation?<LI>How can you represent the number 64 in hexadecimal insidea double-quoted string?<LI>What is the easiest way to represent an array that iNCludesthe numbers 56 to 87?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Write a program that prints the decimal number 32. However,in the print command, specify the value of 32 using hexadecimalnotation.<LI>Create program that uses the tab character in three literalsto align text.<LI>Write a program that prints using embedded new lines in asingle-quoted literal.<LI>Convert the number 56,500,000 into scientific notation.<LI>Write a program that prints an array that uses the range operator.The left value should be AA and the right value should be BB.What happens and why?<LI>Write a program that prints its own source code using a back-quotedstring.</OL><HR><CENTER><P><A HREF="ch1.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch3.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>

⌨️ 快捷键说明

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