Scanning all elements of an array in GAWK returns numbers instead of values
Question
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|
Solution
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])
}
}
OTHER TIPS
Because
for ... in
gives you the keys. Use
printf("|%s|\n",recs[field]);
to get the values.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow