?? ch3.htm
字號:
<P>
Now it's time to look at associative arrays. These are definitely
the most complicated of the three data types. And yet, they are
just another type of array. You've already seen that array elements
can be accessed with both positive and negative integer indexes.
Well, with associative arrays you can use <I>any</I> scalar data
type as an index. Associative array names start with the <TT>%</TT>
character.
<P>
You will see associative arrays called <I>hashes </I>at times.
The term "hash" refers to how associative array elements
are stored in memory. "Hash" also is much shorter than
"associative array," and therefore much easier to type
and talk about.
<H3><A NAME="ExampleAssigningValuestoAssociativeArrayVariables">
Example: Assigning Values to Associative Array Variables</A></H3>
<P>
Before we discuss associative arrays further, let's see how to
assign values to them. When defining a whole array, you can use
the same representation that was used for arrays-just remember
that you need two items for every element in the associative array.
You also can assign values to individual elements of an associative
array by using curly braces (<TT>{}</TT>)
around the index key.
<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>Create an associative array with three elements. Each element
consists of twovalues: the lookup key and its associated value.
<BR>
Add a single element to the associative array.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
%associativeArray = ("Jack A.", "Dec 2", "Joe B.",
"June 2", "Jane C.", "Feb 13");
$associativeArray{"Jennifer S."} = "Mar 20";
print "Joe's birthday is: " . $associativeArray{"Joe B."} . "\n";
print "Jennifer's birthday is: " . $associativeArray{"Jennifer S."} . "\n";
</PRE>
</BLOCKQUOTE>
<P>
This program will print the following:
<BLOCKQUOTE>
<PRE>
Joe's birthday is: June 2
Jennifer's birthday is: Mar 20
</PRE>
</BLOCKQUOTE>
<P>
Perl will extend the associative array as needed when you assign
values to keys. An internal table is used to keep track of which
keys are defined. If you try to access an undefined key, Perl
will return a null or blank string.
<P>
You can do a lot with associative arrays, but first you need more
background in operators, fuNCtions, and statements. We'll handle
these topics in future chapters. In the next section, we look
at string literals and how they interact with variables.
<H2><A NAME="DoubleQuotedStringsRevisited"><FONT SIZE=5 COLOR=#FF0000>
Double-Quoted Strings Revisited</FONT></A></H2>
<P>
Perl strings have some additional fuNCtionality that was not mentioned
in <A HREF="ch1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch1.htm" >Chapter 1</A> "Getting Your Feet Wet," because you needed
to know a little about variables beforehand. Now that you are
familiar with how Perl handles basic variables, let's look a little
deeper at double-quoted strings.
<H3><A NAME="ExampleVariableInterpolation">
Example: Variable Interpolation</A></H3>
<P>
<I>Interpolation</I> is a big word for a simple coNCept-replacement
of a variable name with its value.You already know that variable
names are a "stand-in" for a value. If <TT>$var</TT>
is equal to <TT>10</TT>, the <TT>$var</TT>
+ <TT>20</TT> is really <TT>10
+ 20</TT>. In Perl, this coNCept also is used inside strings.
You can combine variables and strings in a very natural way using
Perl. Simply place the variable directly inside a double-quoted
string, and its value automatically will be interpolated as needed.
<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Until now, each time you printed an array, all of the elements were mashed together (coNCatenated). Having the array element printed without delimiting spaces made determining the individual items very difficult. If, when printing, you eNClose the array
in quotes, Perl automatically will separate the array elements with a space.</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>Create a five-element array.<BR>
Print the element with spaces between the elements.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
@array = (1..5);
print "@array\n";
</PRE>
</BLOCKQUOTE>
<P>
This program will print:
<BLOCKQUOTE>
<PRE>
1 2 3 4 5
</PRE>
</BLOCKQUOTE>
<P>
Perl runs into a problem when you want to use a variable and
then append some letters to the end. Let's illustrate this with
scalar variables.
<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 the value </I><TT><I>large</I></TT><I>
to a scalar variable.<BR>
Print a string with an embedded variable.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$word = "large";
print "He was a $wordr fellow.";
</PRE>
</BLOCKQUOTE>
<P>
This program will print:
<BLOCKQUOTE>
<PRE>
He was a fellow.
</PRE>
</BLOCKQUOTE>
<P>
In this example, Perl looks for the variable <TT>$wordr</TT>-obviously
not what I intended to do. I meant for the string "He was
a larger fellow" to print. This problem can be corrected
by doing the following:
<BLOCKQUOTE>
<PRE>
$word = "large";
print "He was a " . $word . "r fellow.";
</PRE>
</BLOCKQUOTE>
<P>
Because the variable is separate, Perl sees the correct variable
name. Then the string coNCatenation operator joins the three strings
together. This method of programming makes it very easy to see
where the variable is.
<P>
Remember when I said that Perl enables you to do something in
many different ways? You also could do the following:
<BLOCKQUOTE>
<PRE>
print "He was a ${word}r fellow.";
</PRE>
</BLOCKQUOTE>
<P>
The curly braces around the variable name tell Perl where the
name starts and ends.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you're ever on IRC and see <TT><B><FONT FACE="Courier">longhair_</FONT></B></TT> or Kirby Hughes (<TT><B><FONT FACE="Courier">khughes@netcom.com</FONT></B></TT>), tell him I said "thanks." He remembered that curly braces can be used in this
manner.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="ExampleUsingtheIquotISpecialVariable">
Example: Using the <I>$"</I> Special Variable</A></H3>
<P>
Perl has a number of special variables. These variables each have
a predefined meaning. <A HREF="ch12.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch12.htm" >Chapter 12</A>, "Using Special Variables,"
introduces you to quite a few Perl special variables. However,
because we were just looking at strings and arrays, we also should
spend a moment and talk about the <TT>$"</TT>
special variable.
<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 the </I><TT><I>$</I></TT><I>"
special variable to the comma character.<BR>
Create a five-element array.<BR>
Print the element with commas between the elements.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$" = ",";
@array = (1..5);
print "@array\n";
</PRE>
</BLOCKQUOTE>
<P>
This program will print:
<BLOCKQUOTE>
<PRE>
1,2,3,4,5
</PRE>
</BLOCKQUOTE>
<P>
Of course, because <TT>$"</TT>
is a scalar variable you also could assign a longer string to
it. For instaNCe, you could use <TT>$"
= ", "</TT> to add both a comma and a space between
the array elements.
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<P>
This chapter introduced you to the coNCept of variables-places
in computer memory that are used to hold values as your program
runs. They are called variables because you can assign different
values to them as needed.
<P>
You read about three types of variables: scalars, arrays, and
associative arrays. Each variable type has its own unique character
that is used to begin a variable name. Scalars use a <TT>$</TT>,
Arrays use an <TT>@</TT>, and associative
arrays use a <TT>%</TT>.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
When I first started to learn Perl, I found it difficult to remember which character begins which variable type. Then, I saw this chart on the Internet and things became clearer:</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>$ = "the" (singular)<BR>
@ = "those" (plural)<BR>
% = "relationship"</TT>
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Each variable type must start with a different character that
uses a separate namespace. This means that <TT>$varName</TT>
and <TT>@varName</TT> are different
variables. Remember, too, that variable names in Perl are case-sensitive.
<P>
A lot of this chapter looked at assigning values to variables
using the equals (=) sign. We also reviewed how to use positive
and negative subscripts (such as <TT>$array[1]</TT>)
to access array elements. Associative array elements are accessed
a little differently-curly braces are used instead of square braces
(for example, <TT>$associativeArray{"Jack
B."}</TT>).
<P>
And finally, we took another look at double-quoted strings to
see how variable interpolation works. You saw that Perl automatically
replaces variables inside double-quoted strings. When arrays are
printed inside strings, their elements are separated by the value
of <TT>$"</TT>-which is usually
a space.
<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 three basic data types that Perl uses?
<LI>How can you determine the number of elements in an array?
<LI>What is a namespace?
<LI>What is the special variable <TT>$[</TT>
used for?
<LI>What is the special variable <TT>$"</TT>
used for?
<LI>What is the value of a variable when it is first used?
<LI>What is an associative array?
<LI>How can you access associative array elements?
</OL>
<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>
Review Exercises</FONT></A></H2>
<OL>
<LI>Create an array called <TT>@months</TT>.
It should have 12 elements in it with the names of the months
represented as strings.
<LI>Create a string that interpolates that value of the variable
<TT>$numberOfBooks</TT>.
<LI>Using the range operator (..), create an array with the following
elements: 1, 5, 6, 7, 10, 11, 12.
<LI>Using the array created in the last exercise, create a <TT>print</TT>
command to display the last element.
<LI>Create an associative array that holds a list of five music
artists and a rating for them. Use "good," "bad,"
and "indifferent" as the ratings.
<LI>Using the array created in the last exercise, create a <TT>print</TT>
command to display the last element.
</OL>
<HR>
<CENTER><P><A HREF="ch2.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch2.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="ch4.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch4.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 + -