連想配列のソート


これは定石なのかもしれないけれど、裏技的なことも書かれています。Golf を awk でされている方は環境変数を export できればなぁと思っているのではないでしょうか。まぁ、ソースコードを読めば書かれているわけですが・・・。

ここでは Kenny McCormack のものをそのまま載せておきます。

 % gawk 'BEGIN{for (i=1; i<=10; i++) x[sprintf("%02d",i)];for (i in x) print i}'
08
09
10
01
02
03
04
05
06
07
 (note that the above is close to sorted, but that is just by accident)
 % setenv WHINY_USERS 1
 % gawk 'BEGIN{for (i=1; i<=10; i++) x[sprintf("%02d",i)];for (i in x) print i}'
01
02
03
04
05
06
07
08
09
10
 (this is sorted, and not just by accident; by design)

あとは TAWK ができるそうです。今となっては入手困難なので、gawk の範囲で回答してほしかったかな。