global variable and local variable

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值