您的位置:首页 > 其它

global variable and local variable

2011-05-05 10:02 423 查看
awk '
function foo(abc, var){
var=99
print "in the function: abc is " abc " and var is " var
}
BEGIN{
var=33
print "before function call, var is " var
foo(12)
print "after function call, var is " var
}'

before function call, var is 33
in the function: abc is 12 and var is 99
after function call, var is 33

However, the same is NOT true for arrays, as is probably explained somewhere
near the paragraph you quoted.

*********************************************************************

or in another way

---------------------------------------------------------

function myfunction (param, local)
{
print param # the value passed to the function
print local # a locally scoped variable declared in the formal
print global # a global variable
}

The respective function call:

myfunction(some_value)

---------------------------------------------------------

there is an example for the the array argument usage, the web: http://www.funtoo.org/en/articles/linux/awk/3/

----------------------------------------

#!/usr/bin/awk -f
BEGIN {
FS="/t+"
months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
}

function monthdigit(mymonth) {
return (index(months,mymonth)+3)/4
}
function doincome(mybalance) {
mybalance[curmonth,$3] += amount
mybalance[0,$3] += amount
}

function doexpense(mybalance) {
mybalance[curmonth,$2] -= amount
mybalance[0,$2] -= amount
}

function dotransfer(mybalance) {
mybalance[0,$2] -= amount
mybalance[curmonth,$2] -= amount
mybalance[0,$3] += amount
mybalance[curmonth,$3] += amount
}

{
curmonth=monthdigit(substr($1,4,3))
amount=$7

#record all the categories encountered
if ( $2 != "-" )
globcat[$2]="yes"
if ( $3 != "-" )
globcat[$3]="yes"

#tally up the transaction properly
if ( $2 == "-" ) {
if ( $3 == "-" ) {
print "Error: inc and exp fields are both blank!"
exit 1
} else {
#this is income
doincome(balance)
if ( $5 == "Y" )
doincome(balance2)
}
} else if ( $3 == "-" ) {
#this is an expense
doexpense(balance)
if ( $5 == "Y" )
doexpense(balance2)
} else {
#this is a transfer
dotransfer(balance)
if ( $5 == "Y" )
dotransfer(balance2)
}
}

END {
bal=0
bal2=0
for (x in globcat) {
bal=bal+balance[0,x]
bal2=bal2+balance2[0,x]
}
printf("Your available funds: %10.2f/n", bal)
printf("Your account balance: %10.2f/n", bal2)
}

--------------------------------------------

The record formate are:

23 Aug 2000 food - - Y Jimmy's Buffet 30.25

Every field in this file is separated by one or more tabs. After the date (field 1, $1), there are two fields called "expense category" and "income category". When I'm entering an expense like on the above line, I put a four-letter nickname in the exp field, and a "-" (blank entry) in the inc field. This signifies that this particular item is a "food expense" :) Here's what a deposit looks like:

23 Aug 2000    -    inco    -    Y    Boss Man        2001.00

-------------------------------------------

result:

Your available funds: 1174.22
Your account balance: 2399.33
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: