技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

awk里的各种坑

今天又遇到一个,一旦需要定义一个局部数组(awk通过把局部变量定义在函数参数列表来实现局部这一特征)那么这个数组可以正常的操作,但是无法对他取长度,一旦使用length(tempArr)会得到这么一个错误:

fatal: attempt to use array `tempArr' in a scalar context

function test(__ARGVEND__, tempArr)
{ 
    for (i=0; i<10; i++)
    {
        tempArr[i] = i+1
    }
    # it's right

    for(j in tempArr)
    {
        print j " " tempArr[j]
    }
    # it's right too

    print length(tempArr)
    # but , here's error
}

 

 

还有一种更普遍的坑,就是没将变量定义成局部变量!!!

 

 

posted on 2014-02-25 14:34  codestyle  阅读(549)  评论(0编辑  收藏  举报