我有以下测试脚本
/^[^a-zA-Z0-9]/ {
DATEd[$3] = $1
}
END {
print " \"data\": ["
for (i = 0 ; i <= 5; i ++ ) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}
并且正在从此文本文件中读取
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
但它没有打印出我想要的东西,我想要它打印出来
"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"],
]
当它实际上只是打印出来时
"data": [
[0, ""],
[1, ""],
[2, ""],
[3, ""],
[4, ""],
[5, ""],
]
那么为什么 "DATEd[$3] = $1" 是空的呢?
另外如何检查数组的长度?DATEd.length 在这种情况下不起作用。
谢谢
编辑_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___
所以在@Fredrik 和@geekosaur 的帮助下,我来到了某个地方,现在是最后几个问题
1) 脚本现在看起来像这样
/[a-zA-Z0-9]/ {
DATEd[NR-1] = $1
}
END {
print " \"data\": ["
for (i in DATEd) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}
并给出以下输出
"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"],
]
但我希望它看起来像这样
"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"]
]
IE 被排序并删除最后一个 ',' 字符之前的最后一个关闭 ']' 字符。这有可能以一种简单的方式接受吗?=)
谢谢 =)
编辑 3 最终结果_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ____
使用了@geekosaur 和@Fredrik 贡献的组合 =)
{
DATEd[NR-1] = $1; len++
}
END {
print " \"data\": ["
#for (i in DATEd) {
for (i = 0 ; i <= len-1; i ++ ) {
{ print " [" i ", \"" DATEd[i] "\"],"}
}
print " ]"
}