📄 ch09_08.htm
字号:
<html><head><title>The join Function (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_07.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch09_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.8. The join Function</h2><p>The <tt class="literal">join</tt><a name="INDEX-634" /> function doesn't use patterns.So why is it in this chapter? It's here because, in a sense,<tt class="literal">join</tt> performs the opposite function of<tt class="literal">split</tt><a name="INDEX-635" />: <tt class="literal">split</tt> breaks upa string into a number of pieces, and <tt class="literal">join</tt> gluestogether a bunch of pieces to make a single string. The<tt class="literal">join</tt> function looks like this:</p><blockquote><pre class="code">my $result = join $glue, @pieces;</pre></blockquote><p>The first argument to <tt class="literal">join</tt> is the glue, which maybe any string. The remaining arguments are a list of pieces.<tt class="literal">join</tt> puts the glue string between the pieces andreturns the resulting string:</p><blockquote><pre class="code">my $x = join ":", 4, 6, 8, 10, 12; # $x is "4:6:8:10:12"</pre></blockquote><p>In that example, we had five items, so there are only four colons.That is, there are four pieces of glue. The glue shows up onlybetween the pieces, never before or after them. So, there will be onefewer piece of glue than the number of items in the list.</p><p>This means that there may be no glue at all, if the listdoesn't have at least two elements:</p><blockquote><pre class="code">my $y = join "foo", "bar"; # gives just "bar", since no fooglue is neededmy @empty; # empty arraymy $empty = join "baz", @empty; # no items, so it's an empty string</pre></blockquote><p>Using <tt class="literal">$x</tt> from above, we can break up a string andput it back together with a different delimiter:</p><blockquote><pre class="code">my @values = split /:/, $x; # @values is (4, 6, 8, 10, 12)my $z = join "-", @values; # $z is "4-6-8-10-12"</pre></blockquote><p>Although <tt class="literal">split</tt> and <tt class="literal">join</tt> workwell together, don't forget that the first argument to<tt class="literal">join</tt> is always a string, not a pattern.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_07.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="ch09_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.7. The split Operator</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">9.9. Exercises</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 + -