📄 0564-0567.html
字号:
<HTML
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:gawk Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=27 //-->
<!-- PAGES=0545-0582 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0561-0563.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0568-0570.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-564"><P>Page 564</P></A>
<H5><A NAME="ch27_ 23">
Multidimension Arrays
</A></H5>
<P>Although awk doesn't directly support multidimension arrays, it does provide a facility to
simulate them. The distinction is fairly trivial to you as a programmer. You can specify multiple
dimensions in the subscript (within the square brackets) in a form familiar to C programmers:
</P>
<!-- CODE SNIP //-->
<PRE>array[5, 3] = "Mary"
</PRE>
<!-- END CODE SNIP //-->
<P>This is stored in a single-dimension array with the subscript actually stored in the form
5 SUBSEP 3. The predefined variable SUBSEP contains the value of the separator of the subscript
components. It defaults to the double quote
(" or \034) because it is unlikely that the double
quote will appear in the subscript itself. Remember that the double quotes are used to contain a
string; they are not stored as part of the string itself. You can always change
SUBSEP if you need to have the double quote character in your multidimension array subscript.
</P>
<P>If you want to calculate total sales by city and state (or country), you will use a
two-dimension array:
</P>
<!-- CODE SNIP //-->
<PRE>total_sales["Philadelphia", "Pennsylvania"] = 10.15
</PRE>
<!-- END CODE SNIP //-->
<P>You can use the in function within a conditional:
</P>
<!-- CODE SNIP //-->
<PRE>("Wilmington", "Delaware") in total_sales
</PRE>
<!-- END CODE SNIP //-->
<P>You can also use the in function within a loop to step through the various
cities.
</P>
<H4><A NAME="ch27_ 24">
Built-in Numeric Functions
</A></H4>
<P>gawk provides a number of numeric functions to calculate special values.
</P>
<P>Table 27.7 summarizes the built-in numeric functions in
gawk. Earlier versions of awk don't support all these functions.
</P>
<P>Table 27.7. gawk built-in numeric functions.
</P>
<TABLE WIDTH="360">
<TR><TD>
Function
</TD><TD>
Purpose
</TD></TR>
<TR><TD>
atan2(x, y)
</TD><TD>
Returns the arctangent of y/x in radians
</TD></TR>
<TR><TD>
cos(x)
</TD><TD>
Returns the cosine of x in radians
</TD></TR>
<TR><TD>
exp(x)
</TD><TD>
Returns e raised to the x power
</TD></TR>
<TR><TD>
int(x)
</TD><TD>
Returns the value of x truncated to an integer
</TD></TR>
<TR><TD>
log(x)
</TD><TD>
Returns the natural log of x
</TD></TR>
<TR><TD>
rand()
</TD><TD>
Returns a random number between 0 and 1
</TD></TR>
<TR><TD>
sin(x)
</TD><TD>
Returns the sine of x in radians
</TD></TR>
<TR><TD>
sqrt(x)
</TD><TD>
Returns the square root of x
</TD></TR>
</TABLE>
<A NAME="PAGENUM-565"><P>Page 565</P></A>
<TABLE WIDTH="360">
<TR><TD>
Function
</TD><TD>
Purpose
</TD></TR>
<TR><TD>
srand(x)
</TD><TD>
Initializes (seeds) the random number generator;
systime() <BR>
is used if x is omitted
</TD></TR>
<TR><TD>
systime()
</TD><TD>
Returns the current time in seconds since midnight, January 1, 1970
</TD></TR>
</TABLE>
<H4><A NAME="ch27_ 25">
Arithmetic Operators
</A></H4>
<P>gawk supports a wide variety of math operations. Table 27.8 summarizes these operators.
</P>
<P>Table 27.8. gawk arithmetic operators.
</P>
<TABLE WIDTH="360">
<TR><TD>
Operator
</TD><TD>
Purpose
</TD></TR>
<TR><TD>
x^y
</TD><TD>
Raises x to the y power
</TD></TR>
<TR><TD>
x**y
</TD><TD>
Raises x to the y power (same as x^y)
</TD></TR>
<TR><TD>
x%y
</TD><TD>
Calculates the remainder of x/y
</TD></TR>
<TR><TD>
x+y
</TD><TD>
Adds x to y
</TD></TR>
<TR><TD>
x-y
</TD><TD>
Subtracts y from x
</TD></TR>
<TR><TD>
x*y
</TD><TD>
Multiplies x times y
</TD></TR>
<TR><TD>
x/y
</TD><TD>
Divides x by y
</TD></TR>
<TR><TD>
-y
</TD><TD>
Negates y (switches the sign of y); also known as the unary minus
</TD></TR>
<TR><TD>
++y
</TD><TD>
Increments y by 1 and uses value (prefix increment)
</TD></TR>
<TR><TD>
y++
</TD><TD>
Uses value of y and then increments by 1 (postfix increment)
</TD></TR>
<TR><TD>
--y
</TD><TD>
Decrements y by 1 and uses value (prefix decrement)
</TD></TR>
<TR><TD>
y--
</TD><TD>
Uses value of y and then decrements by 1 (postfix decrement)
</TD></TR>
<TR><TD>
x=y
</TD><TD>
Assigns value of y to x. gawk also supports operator-<BR>assignment
operators (+=, -=, *=, /=, %=, ^=, and **=)
</TD></TR>
</TABLE>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
All math in gawk uses floating point (even if you treat the number as an
integer).
</BLOCKQUOTE></TD></TR>
</TABLE>
</P>
<H4>
Conditional Flow
</H4>
<P>By its very nature, an action within a gawk program is conditional. It is executed if its
pattern is true. You can also have conditional programs flow within the action through the use of an
if statement.
</P>
<A NAME="PAGENUM-566"><P>Page 566</P></A>
<P>The general flow of an if statement is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>if (condition)
statement to execute when true
else
statement to execute when false
</PRE>
<!-- END CODE SNIP //-->
<P>condition can be any valid combination of patterns shown in Tables 27.2 and 27.3.
else is optional. If you have more than one statement to execute, you need to enclose the
statements within curly braces ({ }), just as in the C syntax.
</P>
<P>You can also stack if and else statements as necessary:
</P>
<!-- CODE //-->
<PRE>if ("Pennsylvania" in total_sales)
print "We have Pennsylvania data"
else if ("Delaware" in total_sales)
print "We have Delaware data"
else if (current_year < 2010)
print "Uranus is still a planet"
else
print "none of the conditions were met."
</PRE>
<!-- END CODE //-->
<H5><A NAME="ch27_ 26">
The Null Statement
</A></H5>
<P>By definition, if requires one (or more) statements to execute; in some cases, the logic
might be straightforward when coded so that the code you want executed occurs when the
condition is false. I have used this when it would be difficult or ugly to reverse the logic to execute
the code when the condition is true.
</P>
<P>The solution to this problem is easy: Just use the null statement, the semicolon
(;). The null statement satisfies the syntax requirement that
if requires statements to execute; it just does nothing.
</P>
<P>Your code will look something like the following:
</P>
<!-- CODE SNIP //-->
<PRE>if (($1 <= 5 && $2 > 3) || ($1 > 7 && $2 < 2))
; # The Null Statement
else
the code I really want to execute
</PRE>
<!-- END CODE SNIP //-->
<H5><A NAME="ch27_ 27">
The Conditional Operator
</A></H5>
<P>gawk has one operator that actually has three parameters: the conditional operator. This
operator allows you to apply an if-test anywhere in your code.
</P>
<P>The general format of the conditional statement is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>condition ? true-result : false-result
</PRE>
<!-- END CODE SNIP //-->
<P>While this might seem like duplication of the
if statement, it can make your code easier to read. If you have a data file that consists of an employee name and the number of sick
days taken, you can use the following:
</P>
<!-- CODE SNIP //-->
<PRE>{ print $1, "has taken", $2, "day" $2 != 1 ? "s" : "", "of sick time" }
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-567"><P>Page 567</P></A>
<P>This prints day if the employee only took one day of sick time and prints
days if the employee took zero or more than one day of sick time. The resulting sentence is more readable. To
code the same example using an if statement would be more complex and look like the following:
</P>
<!-- CODE SNIP //-->
<PRE>if ($2 != 1)
print $1, "has taken", $2, "days of sick time"
else
print $1, "has taken", $2, "day of sick time"
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch27_ 28">
Looping
</A></H4>
<P>By their very nature, awk programs are one big loop—reading each record in the input file
and processing the appropriate patterns and actions. Within an action, the need for repetition
often occurs. awk supports loops through the do,
for, and while statements that are similar to those found in C.
</P>
<P>As with the if statement, if you want to execute multiple statements within a loop, you
must contain them in curly braces.
</P>
<P>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
TIP
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Forgetting the curly braces around multiple statements is a common programming error
with conditional and looping statements.
</BLOCKQUOTE></TD></TR>
</TABLE>
</P>
<H5>
The do Statement
</H5>
<P>The do statement (sometimes referred to as the do
while statement) provides a looping construct that will be executed at least once. The condition or test occurs after the contents of
the loop have been executed.
</P>
<P>The do statement takes the following form:
</P>
<!-- CODE SNIP //-->
<PRE>do
statement
while (condition)
</PRE>
<!-- END CODE SNIP //-->
<P>statement can be one statement or multiple statements enclosed in curly braces.
condition is any valid test like those used with the
if statement or the pattern used to trigger actions.
</P>
<P>In general, you must change the value of the variable in the condition within the loop. If
you don't, you will have a loop forever condition because the test result
(condition) would never change (and become false).
</P>
<H5><A NAME="ch27_ 29">
Loop Control
</A></H5>
<P>You can exit a loop early if you need to (without assigning some bogus value to the variable
in the condition). awk provides two facilities to do this:
break and continue.
</P>
<P><CENTER>
<a href="0561-0563.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0568-0570.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -