📄 ch2.htm
字号:
be 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. You
could use an array to hold all of the lines in a file, to help
sort a list of addresses, or to store a variety of items. We'll
look at some simple arrays in this section. In the next chapter,
"Variables," you'll see more examples of how useful
arrays 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 arrays
are represented in Perl source code.
<P>
This example shows an empty array, an array of numbers and an
array of strings.
<P>
Figure 2.4 shows the output of Listing 2.3.
<P>
<A HREF="f2-4.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f2-4.gif"><B>Figure 2.4 : </B><I>The output from Listing 2.3, showing different
array literals</I>.</A>
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 02LST03.PL-Printing Some Array Literals
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
print "Here is an empty array:" . () . "<-- Nothing there!\n";
print (12, 014, 0x0c, 34.34, 23.3E-3);
print "\n";
print ("This", "is", 'an', "array", 'of', "strings");
print "\n";
print ("This", 30, "is", 'a', "mixed array", 'of', 0x08, "items");.
</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 mix
numbers 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>"Here is an empty array:"</TT> and the string <TT>"<-- Nothing there!\n"</TT>. You can read more
about operators in <A HREF="ch4.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch4.htm" >Chapter 4</A> "Operators."
</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 "Strings Revisited" 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>
<H3><A NAME="ExampleNestingArrays">
Example: Nesting Arrays</A></H3>
<P>
Many times a simple list is not enough. If you're a painter, you
might have one array that holds the names of orange hues and one
that holds the names of yellow hues. To print them, you can use
Perl's ability to specify a sub-array inside your main array definition.
<P>
While this example is not very "real-world," it gives
you the idea behind specifying an array by using sub-arrays.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 another
array.</I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
print (("Bright Orange", "Burnt"), ("Canary Yellow", "Sunbeam"));
print (("Bright Orange", "Burnt"), " Middle ", ("Canary Yellow",
"Sunbeam"));
</PRE>
</BLOCKQUOTE>
<P>
So far, we haven't talked about the internal representations of
data types. That's because you almost never have to worry about
such things with Perl. However, it is important to know that,
internally, the sub-arrays are merged into the main array. In
other words, the <TT>array:</TT>
<BLOCKQUOTE>
<PRE>
(("Bright Orange", "Burnt"), ("Canary Yellow", "Sunbeam"))
</PRE>
</BLOCKQUOTE>
<P>
is exactly equivalent to
<BLOCKQUOTE>
<PRE>
("Bright Orange", "Burnt", "Canary Yellow", "Sunbeam")
</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 numbers
or letters. Instead of making you list out the entire array, Perl
has a shorthand notation that you can use.
<P>
Perl uses two periods (..) to replace a consecutive series of
values. Not only is this method quicker to type-and less prone
to error-it is easier to understand. Only the end points of the
series are specified; you don't need to manually verify that every
value is represented. If the .. is used, then automatically you
know that a range of values will be used.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 using
the shorthand method.</I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
print (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
print "\n";
print (1..15);
</PRE>
</BLOCKQUOTE>
<P>
The two arrays used in the previous example are identical, but
they 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch4.htm" >Chapter 4</A> "Operators."
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
You can also use the shorthand method to specify values in the
middle of an array.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 "\n"
print ("A", "B", "F".."H", "Y", "Z");
</PRE>
</BLOCKQUOTE>
<P>
The range operator works by taking the lefthand value, adding
one to it, then appending that new value to the array. Perl continues
to do this until the new value reaches the righthand value. You
can use letters with the range operator because the ASCII table
uses consecutive values to represent consecutive letters.
<P>
For more information about ASCII codes, see Appendix E, "ASCII
Table."
<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 directly
into your source code and never changed by the program. They are
sometimes referred to as hard-coded values.
<P>
You read about numbers and the three different bases that can
be used to represent them-decimal, octal, and hexadecimal. Very
large or small numbers can also be described using scientific
notation.
<P>
Strings were perhaps a bit more involved. Single-, double-, and
back-quoted strings are used to hold strings of characters. Back-quoted
strings have an additional purpose. They tell Perl to send the
string to the operating system for execution.
<P>
Escape sequeNCes are used to represent characters that are difficult
to enter through the keyboard or that have more than one purpose.
For example, using a double quote inside a double-quoted string
would end the string before you really intended. The backslash
character was introduced to escape the double quote and change
its meaning.
<P>
The next chapter, "Variables," will show you how Perl
uses your computer memory to store data types and also will show
you 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-quoted
strings?
<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 inside
a double-quoted string?
<LI>What is the easiest way to represent an array that iNCludes
the 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 hexadecimal
notation.
<LI>Create program that uses the tab character in three literals
to align text.
<LI>Write a program that prints using embedded new lines in a
single-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-quoted
string.
</OL>
<HR>
<CENTER><P><A HREF="ch1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch1.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 + -