I have the following test script
/^[^a-zA-Z0-9]/ {
DATEd[$3] = $1
}
END {
print " \"data\": ["
for (i = 0 ; i <= 5; i ++ ) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}
And are reading from this text file
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
But it doesn't print out what I want it to, I want it to print out
"data": [
[0, "2011-01-22"],
[1, "2011-01-22"],
[2, "2011-01-22"],
[3, "2011-01-22"],
[4, "2011-01-22"],
[5, "2011-01-22"],
]
When it in fact are only printing out
"data": [
[0, ""],
[1, ""],
[2, ""],
[3, ""],
[4, ""],
[5, ""],
]
So why is "DATEd[$3] = $1" empty?
Also how do I check the length of an array? DATEd.length doesn't work in this case.
Thanks
EDIT_______________________________________________
So from the help of @Fredrik and @geekosaur I have come somewhere with this, now to some last questions
1) The script now looks like this
/[a-zA-Z0-9]/ {
DATEd[NR-1] = $1
}
END {
print " \"data\": ["
for (i in DATEd) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}
And gives the following output
"data": [
[4, "2011-01-26"],
[5, "2011-01-27"],
[6, "2011-01-28"],
[0, "2011-01-22"],
[1, "2011-01-23"],
[2, "2011-01-24"],
[3, "2011-01-25"],
]
But I want it to look like this
"data": [
[0, "2011-01-22"],
[1, "2011-01-23"],
[2, "2011-01-24"],
[3, "2011-01-25"],
[4, "2011-01-26"],
[5, "2011-01-27"],
[6, "2011-01-28"]
]
I.E be sorted and removing the last ',' character before the final closing ']' character. Is this possible to accieve in a easy way? =)
Thanks =)
EDIT 3 Final Outcome_______________________________________
Used a combination of @geekosaur and @Fredrik contribution's =)
{
DATEd[NR-1] = $1; len++
}
END {
print " \"data\": ["
#for (i in DATEd) {
for (i = 0 ; i <= len-1; i ++ ) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}