0

给定以下功能:

function process_pipes(text)
{
    split(text,recs,"|");
    for (field in recs){
        printf ("|%s|\n", field)
    }
}

如果输入为:0987654321|57300|ERROR account number not found|GDUMARESQ|0199|9|N|0||

为什么我得到下面的数字而不是文本?

|4|
|5|
|6|
|7|
|8|
|9|
|10|
|1|
|2|
|3|
4

2 回答 2

2

因为

for ... in 

给你钥匙。采用

printf("|%s|\n",recs[field]);

获取值。

于 2009-06-18T23:02:21.963 回答
2

splitrecs在您的代码中创建一个数组,并且recs[1]== 0987654321 等。

for (field in recs)循环生成索引列表,而不是数组元素。

因此,您需要:

function process_pipes(text)
{
    split(text,recs,"|");
    for (field in recs){
        printf ("|%s|\n", recs[field])
    }
}
于 2009-06-18T23:04:56.483 回答