当前位置: 动力学知识库 > 问答 > 编程问答 >

string - Scanning all elements of an array in GAWK returns numbers instead of values

问题描述:

Given the following function:

function process_pipes(text)

{

split(text,recs,"|");

for (field in recs){

printf ("|%s|\n", field)

}

}

If the input is: 0987654321|57300|ERROR account number not found|GDUMARESQ|0199|9|N|0||

Why do I get the numbers below instead of the text?

|4|

|5|

|6|

|7|

|8|

|9|

|10|

|1|

|2|

|3|

网友答案:

split creates an array recs in your code, and recs[1] == 0987654321, etc.

The for (field in recs) loop generates the list of indexes, not the array elements.

Hence, you need:

function process_pipes(text)
{
    split(text,recs,"|");
    for (field in recs){
        printf ("|%s|\n", recs[field])
    }
}
网友答案:

Because

for ... in 

gives you the keys. Use

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

to get the values.

分享给朋友:
您可能感兴趣的文章:
随机阅读: