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

📄 ch3.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P>Now it's time to look at associative arrays. These are definitelythe most complicated of the three data types. And yet, they arejust another type of array. You've already seen that array elementscan be accessed with both positive and negative integer indexes.Well, with associative arrays you can use <I>any</I> scalar datatype 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 &quot;hash&quot; refers to how associative array elementsare stored in memory. &quot;Hash&quot; also is much shorter than&quot;associative array,&quot; and therefore much easier to typeand 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 toassign values to them. When defining a whole array, you can usethe same representation that was used for arrays-just rememberthat you need two items for every element in the associative array.You also can assign values to individual elements of an associativearray by using curly braces (<TT>{}</TT>)around the index key.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Create an associative array with three elements. Each elementconsists of twovalues: the lookup key and its associated value.<BR>Add a single element to the associative array.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>%associativeArray = (&quot;Jack A.&quot;, &quot;Dec 2&quot;, &quot;Joe B.&quot;,    &quot;June 2&quot;, &quot;Jane C.&quot;, &quot;Feb 13&quot;);$associativeArray{&quot;Jennifer S.&quot;} = &quot;Mar 20&quot;;print &quot;Joe's birthday is: &quot; . $associativeArray{&quot;Joe B.&quot;} . &quot;\n&quot;;print &quot;Jennifer's birthday is: &quot; . $associativeArray{&quot;Jennifer S.&quot;} . &quot;\n&quot;;</PRE></BLOCKQUOTE><P>This program will print the following:<BLOCKQUOTE><PRE>Joe's birthday is: June 2Jennifer's birthday is: Mar 20</PRE></BLOCKQUOTE><P>Perl will extend the associative array as needed when you assignvalues to keys. An internal table is used to keep track of whichkeys are defined. If you try to access an undefined key, Perlwill return a null or blank string.<P>You can do a lot with associative arrays, but first you need morebackground in operators, fuNCtions, and statements. We'll handlethese topics in future chapters. In the next section, we lookat 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 mentionedin <A HREF="ch1.htm" >Chapter 1</A> &quot;Getting Your Feet Wet,&quot; because you neededto know a little about variables beforehand. Now that you arefamiliar with how Perl handles basic variables, let's look a littledeeper 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-replacementof a variable name with its value.You already know that variablenames are a &quot;stand-in&quot; 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 usingPerl. Simply place the variable directly inside a double-quotedstring, 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" 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 &quot;@array\n&quot;;</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 andthen append some letters to the end. Let's illustrate this withscalar variables.<P><IMG SRC="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 = &quot;large&quot;;print &quot;He was a $wordr fellow.&quot;;</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>-obviouslynot what I intended to do. I meant for the string &quot;He wasa larger fellow&quot; to print. This problem can be correctedby doing the following:<BLOCKQUOTE><PRE>$word = &quot;large&quot;;print &quot;He was a &quot; . $word . &quot;r fellow.&quot;;</PRE></BLOCKQUOTE><P>Because the variable is separate, Perl sees the correct variablename. Then the string coNCatenation operator joins the three stringstogether. This method of programming makes it very easy to seewhere the variable is.<P>Remember when I said that Perl enables you to do something inmany different ways? You also could do the following:<BLOCKQUOTE><PRE>print &quot;He was a ${word}r fellow.&quot;;</PRE></BLOCKQUOTE><P>The curly braces around the variable name tell Perl where thename 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 &quot;thanks.&quot; 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>$&quot;</I> Special Variable</A></H3><P>Perl has a number of special variables. These variables each havea predefined meaning. <A HREF="ch12.htm" >Chapter 12</A>, &quot;Using Special Variables,&quot;introduces you to quite a few Perl special variables. However,because we were just looking at strings and arrays, we also shouldspend a moment and talk about the <TT>$&quot;</TT>special variable.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Set the </I><TT><I>$</I></TT><I>&quot;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>$&quot; = &quot;,&quot;;@array = (1..5);print &quot;@array\n&quot;;</PRE></BLOCKQUOTE><P>This program will print:<BLOCKQUOTE><PRE>1,2,3,4,5</PRE></BLOCKQUOTE><P>Of course, because <TT>$&quot;</TT>is a scalar variable you also could assign a longer string toit. For instaNCe, you could use <TT>$&quot;= &quot;, &quot;</TT> to add both a comma and a space betweenthe 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-placesin computer memory that are used to hold values as your programruns. They are called variables because you can assign differentvalues to them as needed.<P>You read about three types of variables: scalars, arrays, andassociative arrays. Each variable type has its own unique characterthat is used to begin a variable name. Scalars use a <TT>$</TT>,Arrays use an <TT>@</TT>, and associativearrays 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>$ = &quot;the&quot; (singular)<BR>@ = &quot;those&quot; (plural)<BR>% = &quot;relationship&quot;</TT></BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Each variable type must start with a different character thatuses a separate namespace. This means that <TT>$varName</TT>and <TT>@varName</TT> are differentvariables. Remember, too, that variable names in Perl are case-sensitive.<P>A lot of this chapter looked at assigning values to variablesusing the equals (=) sign. We also reviewed how to use positiveand negative subscripts (such as <TT>$array[1]</TT>)to access array elements. Associative array elements are accesseda little differently-curly braces are used instead of square braces(for example, <TT>$associativeArray{&quot;JackB.&quot;}</TT>).<P>And finally, we took another look at double-quoted strings tosee how variable interpolation works. You saw that Perl automaticallyreplaces variables inside double-quoted strings. When arrays areprinted inside strings, their elements are separated by the valueof <TT>$&quot;</TT>-which is usuallya 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>$&quot;</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 monthsrepresented 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 followingelements: 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 musicartists and a rating for them. Use &quot;good,&quot; &quot;bad,&quot;and &quot;indifferent&quot; 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"><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="ch4.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 + -