?? ch11_06.htm
字號:
<html><head><title>Variables and Array Assignments (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><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="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><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="ch11_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch11_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">11.6. Variables and Array Assignments</h2><p><a name="IXT-11-123306" /><a name="IXT-11-123307" />Variables can be assigned a value with an = sign. For example:</p><blockquote><pre class="code">FS = ","</pre></blockquote><p>Expressions using the operators <tt class="literal">+</tt>,<tt class="literal">-</tt>, <tt class="literal">/</tt>, and <tt class="literal">%</tt> (modulo) can be assigned to variables.</p><p>Arrays can be created with the <tt class="literal">split()</tt> function (seebelow), or they can simply be named in an assignment statement. Arrayelements can be subscripted with numbers (<em class="emphasis">array</em><tt class="literal">[1]</tt>, ...,<em class="emphasis">array</em><tt class="literal">[</tt><em class="emphasis">n</em><tt class="literal">]</tt>) orwith strings.Arrays subscripted by strings arecalled <em class="emphasis">associative arrays.</em><a href="#FOOTNOTE-11">[11]</a>For example, to count the number of widgets you have,you could use the following script:</p><blockquote class="footnote"><a name="FOOTNOTE-11" /><p> [11]In fact, all arrays in <tt class="literal">awk</tt> are associative;numeric subscripts are converted to strings before using themas array subscripts.Associative arrays are one of <tt class="literal">awk</tt>'s mostpowerful features.</p></blockquote><blockquote><pre class="code">/widget/ { count["widget"]++ } <i class="lineannotation">Count widgets</i>END { print count["widget"] } <i class="lineannotation">Print the count</i></pre></blockquote><p>You can use the special <tt class="literal">for</tt> loop to read all theelements of an associative array:</p><blockquote><pre class="code">for (item in array) <em class="replaceable"><tt>process</tt></em> array[item]</pre></blockquote><p>The index of the array is available as<tt class="literal">item</tt>, while the value of an element of thearray can be referenced as<tt class="literal">array[item]</tt>.</p><p>You can use the operator <tt class="literal">in</tt> to see if an elementexists by testing to see if its index exists (<tt class="literal">nawk</tt> only):</p><blockquote><pre class="code">if (index in array) ...</pre></blockquote><p>This sequence tests that<tt class="literal">array[index]</tt>exists, but you cannot use it to test the value of the elementreferenced by<tt class="literal">array[index]</tt>.</p><p>You can also delete individual elements of the array using the<tt class="literal">delete</tt> statement (<tt class="literal">nawk</tt> only).</p><a name="unut-ch-11-sect-6.1" /><div class="sect2"><h3 class="sect2">11.6.1. Escape Sequences</h3><p>Within string and regular expression constants,the following escape sequences may be used. Note: The <tt class="literal">\x</tt> escape sequence is a commonextension; it is not part of POSIX <tt class="literal">awk</tt>.</p><table border="1" cellpadding="3"><tr><th>Sequence</th><th>Meaning</th><th>Sequence</th><th>Meaning</th></tr><tr><td><tt class="literal">\a</tt></td><td>Alert (bell)</td><td><tt class="literal">\v</tt></td><td>Vertical tab</td></tr><tr><td><tt class="literal">\b</tt></td><td>Backspace</td><td><tt class="literal">\\</tt></td><td>Literal backslash</td></tr><tr><td><tt class="literal">\f</tt></td><td>Form feed</td><td><tt class="literal">\</tt><em class="replaceable"><tt>nnn</em></td><td>Octal value <em class="emphasis">nnn</tt></em></td></tr><tr><td><tt class="literal">\n</tt></td><td>Newline</td><td><tt class="literal">\x</tt><em class="replaceable"><tt>nn</em></td><td>Hexadecimal value <em class="emphasis">nn</tt></em></td></tr><tr><td><tt class="literal">\r</tt></td><td>Carriage return</td><td><tt class="literal">\"</tt></td><td>Literal double quote (in strings)</td></tr><tr><td><tt class="literal">\t</tt></td><td>Tab</td><td><tt class="literal">\/</tt></td><td>Literal slash (in regular expressions)</td></tr></table><p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch11_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">11.5. Operators</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">11.7. User-Defined Functions</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2003</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"></map></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -