📄 ch17_06.htm
字号:
Actually, in a linguistic sense, it's more like a pluralmarker, much like the letter "s" in words like"cats" and "dogs." In Perl, the dollar signmeans there's just one of something, but the at-sign meansthere's a list of items.</p><p>A slice is always a list, so the array slice notation uses an at-signto indicate that. When you see something like <tt class="literal">@names[ ...]</tt> in a Perl program, you'll need to do just as Perldoes and look at the at-sign at the beginning as well as the squarebrackets at the end. The square brackets mean that you'reindexing into an array, and the at-sign means that you'regetting a whole list<a href="#FOOTNOTE-374">[374]</a>of elements, not just a single one (which is what the dollar signwould mean). See <a href="ch17_06.htm#lperl3-CHP-17-FIG-1">Figure 17-1</a>.</p><blockquote class="footnote"> <a name="FOOTNOTE-374" /><p>[374]Of course, when we say "awhole list," that doesn't necessarily mean more elementsthan one -- the list could be empty, after all.</p> </blockquote><a name="lperl3-CHP-17-FIG-1" /><div class="figure"><img height="191" alt="Figure 17-1" src="figs/lrnp_1701.gif" width="161" /></div><h4 class="objtitle">Figure 17-1. Array slices versus single elements</h4><p>The punctuation mark at the front of the variable reference (eitherthe dollar sign or at-sign) determines the context of the subscriptexpression. If there's a dollar sign in front, the subscriptexpression is evaluated in a scalar context to get an index. But ifthere's an at-sign in front, the subscript expression isevaluated in a list context to get a list of indices.</p><p>So we see that <tt class="literal">@names[ 2, 5 ]</tt> means the same listas <tt class="literal">($names[2],</tt> <tt class="literal">$names[5])</tt> does.If you want that list of values, you can simply use the array slicenotation. Any place you might want to write the list, you can insteaduse the simpler array slice.</p><p>But the slice can be used in one place where the list can't: aslice may be interpolated directly into a string:</p><blockquote><pre class="code">my @names = qw{ zero one two three four five six seven eight nine };print "Bedrock @names[ 9, 0, 2, 1, 0 ]\n";</pre></blockquote><p>If we were to interpolate <tt class="literal">@names</tt>, that would giveall of the items from the array, separated by spaces. If instead weinterpolate <tt class="literal">@names[ 9, 0, 2, 1, 0 ]</tt>, that givesjust those items from the array, separated by spaces.<a href="#FOOTNOTE-375">[375]</a></p><blockquote class="footnote"><a name="FOOTNOTE-375" /><p>[375]More accurately, the items of the list are separated by thecontents of Perl's <tt class="literal">$"</tt> variable, whosedefault is a space. This should not normally be changed. Wheninterpolating a list of values, Perl internally does <tt class="literal">join$", @list</tt>, where <tt class="literal">@list</tt> stands in for thelist expression. </p> </blockquote><p>Let's go back to the Bedrock Library for a moment. Maybe nowour program is updating Mr. Slate's address and phone number inthe patron file, because he just moved into a large new place in theHollyrock hills. If we've got a list of information about himin <tt class="literal">@items</tt>, we could do something like this toupdate just those two elements of the array:</p><blockquote><pre class="code">my $new_home_phone = "555-6099";my $new_address = "99380 Red Rock West";@items[2, 3] = ($new_address, $new_home_phone);</pre></blockquote><p>Once again, the array slice makes a more compact notation for a listof elements. In this case, that last line is the same as anassignment to <tt class="literal">($items[2],</tt><tt class="literal">$items[3])</tt>, but more compact and efficient.</p></div><a name="lperl3-CHP-17-SECT-6.2" /><div class="sect2"><h3 class="sect2">17.6.2. Hash Slice</h3><p>In a way exactly analogous to an array slice, we can also slice someelements from a <a name="INDEX-1125" />hash in a <em class="firstterm">hashslice</em>. Remember when three of our characters wentbowling, and we kept their bowling scores in the<tt class="literal">%score</tt> hash? We could pull those scores with alist of hash elements or with a slice. These two techniques areequivalent, although the second is more concise and efficient:</p><blockquote><pre class="code">my @three_scores = ($score{"barney"}, $score{"fred"}, $score{"dino"});my @three_scores = @score{ qw/ barney fred dino/ };</pre></blockquote><p>A slice is always a list, so the hash slice notation uses an at-signto indicate that.<a href="#FOOTNOTE-376">[376]</a> When you see something like <tt class="literal">@score{ ...}</tt> in a Perl program, you'll need to do just as Perldoes and look at the at-sign at the beginning as well as the curlybraces at the end. The curly braces mean that you're indexinginto a hash; the at-sign means that you're getting a whole listof elements, not just a single one (which is what the dollar signwould mean). See <a href="ch17_06.htm#lperl3-CHP-17-FIG-2">Figure 17-2</a>.</p><blockquote class="footnote"> <a name="FOOTNOTE-376" /><p>[376]If it sounds as if we'rerepeating ourselves here, it's because we want to emphasizethat hash slices are analogous to array slices. If it sounds as ifwe're not repeating ourselves here, it's because we wantto emphasize that hash slices are analogous to array slices. </p></blockquote><a name="lperl3-CHP-17-FIG-2" /><div class="figure"><img height="191" alt="Figure 17-2" src="figs/lrnp_1702.gif" width="150" /></div><h4 class="objtitle">Figure 17-2. Hash slices versus single elements</h4><p>As we saw with the array slice, the punctuation mark at the front ofthe variable reference (either the dollar sign or at-sign) determinesthe context of the subscript expression. If there's a dollarsign in front, the subscript expression is evaluated in a scalarcontext to get a single key.<a href="#FOOTNOTE-377">[377]</a> But if there's an at-sign in front, thesubscript expression is evaluated in a list context to get a list ofkeys.</p><blockquote class="footnote"> <a name="FOOTNOTE-377" /><p>[377]There's anexception you're not likely to run across, since it isn'tused much in modern Perl code. See the entry for<tt class="literal">$;</tt> in the <em class="emphasis">perlvar</em> manpage.</p> </blockquote><p>It's normal at this point to wonder why there's nopercent sign ("<tt class="literal">%</tt>") here, whenwe're talking about a hash. That's the marker that meansthere's a whole hash; a hash slice (like any other slice) isalways a <em class="emphasis">list</em>, not a hash.<a href="#FOOTNOTE-378">[378]</a> In Perl, the dollar sign meansthere's just one of something, but the at-sign meansthere's a list of items, and the percent sign meansthere's an entire hash.</p><blockquote class="footnote"> <a name="FOOTNOTE-378" /><p>[378]Ahash slice is a slice (not a hash) in the same way that a house fireis a fire (not a house), while a fire house is a house (not a fire).More or less.</p> </blockquote><p>As we saw with array slices, a hash slice may be used instead of thecorresponding list of elements from the hash, anywhere within Perl.So we can set our friends' bowling scores in the hash (withoutdisturbing any other elements in the hash) in this simple way:</p><blockquote><pre class="code">my @players = qw/ barney fred dino /;my @bowling_scores = (195, 205, 30);@score{ @players } = @bowling_scores;</pre></blockquote><p>That last line does the same thing as if we had assigned to thethree-element list <tt class="literal">($score{"barney"}, $score{"fred"},$score{"dino"})</tt>.</p><p>A hash slice may be interpolated, too. Here, we print out the scoresfor our favorite bowlers:</p><a name="INDEX-1126" /><blockquote><pre class="code">print "Tonight's players were: @players\n";print "Their scores were: @score{@players}\n";</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch17_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch17_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">17.5. More Powerful Regular Expressions</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">17.7. Exercise</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -