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:
result:
Your available funds: 1174.22
Your account balance: 2399.33
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
相关文章推荐
- Ruby 中的各种变量(local/instance/class/global variable and assignment method)
- global variable and local variable
- python global and local variable
- Let there be Color!: Joint End-to-end Learning of Global and Local Image Priors for Automatic Image
- GLOBAL variable and should be set with SET GLOBAL
- Can we access global variable if there is a local variable with same name?
- [转]Difference among Domain Local Group and Global Group and Universal Group
- None of the XX first of XX (0) points of the global plan were in the local costmap and free
- 姿态论文整理--04-Human pose estimation using global and local normalization
- 论文笔记:Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language Association
- C++中关于静态对象的使用( local static object and global static object)
- globe variable and local variable testing
- 775. Global and Local Inversions
- What is Local Index and Global Index in Partition Table? What is Prefix Index and Nonprefix Index in table?
- C++中关于静态对象的使用( local static object and global static object)
- global and local
- Difference among Domain Local Group and Global Group and Universal Group
- Learning with Local and Global Consistency
- Questions about OPENCL global and local work size
- Local and Global References