我想做类似的事情:
for (x in {a,b}) {
...
}
有没有办法在 awk 中做到这一点?
两种选择:
1)
for (x in a)
u[x]
for (x in b)
u[x]
for (x in u)
print "Union Index:",x
2)
for (x in a)
print "Union Index:",x
for (x in b)
if (!(x in a))
print "Union Index:",x
如果你想要一些东西,你可以在 for 循环中简洁地使用:
$ cat tst.awk
function indices(a,b,u, x,c)
{
for (x in a) {
u[++c] = x
}
for (x in b) {
if (!(x in a)) {
u[++c] = x
}
}
return c
}
BEGIN {
a[3]="foo"
a[9]=3
b[5]=7
b[15]=45
for (i=1; i<=indices(a,b,u); i++) {
print u[i]
}
}
$ awk -f tst.awk
9
3
5
15