4 ms·
Ah! This makes me wish for an awk with first class channels and loadable modules. Gawk does allow talking over sockets but this would have been so much sweeter.
by srean 8y ago
Ah! This makes me wish for an awk with first class channels and loadable modules. Gawk does allow talking over sockets but this would have been so much sweeter.
One thing hope this implementation remedies is the absence of a linear time string concatenation in awk. Awk has split but no join. Only way I know is to iteratively join two strings which has a quadratic running time.
- benhoyt 8y agoHuh, interesting. So one thing I haven't focused on much yet is performance. It is slightly faster than "one true awk" on large inputs with very simple programs, so I'm guessing Go's I/O speed is pretty good, but the actual interpreter itself is significantly slower than awk's as yet -- hoping to work on that soon. I haven't looked at linear-time string concat. Interesting point -- I'll put it on my TODO list. Though I think instead of string building you could simply use printf to write output and that would be linear time.
- srean 8y agoAs they say make it work and then make it fast. Fair warning, mawk will give you tough competition on speed. Gawk should be possible to beat, its implementation is not very performance focused. Regarding concat, printf and sprintf would indeed work in some situations. Its not flexible enough in the following scenario: Consider an array of substrings, possibly generated by 'split'. I modify and delete some of them. Then I want to put them back together. Since i do not know ahead of time how many substrings will survive, sprintf is difficult to use.
- kiwidrew 8y agoYes, if the "destination" is stdout (or a pipe/file) then the obvious loop works just fine: function join(ARRAY) { for (i=0; i in ARRAY; i++) printf "%s", ARRAY[i]; } But if you need the result back as a string for further processing, the obvious methods are not linear: function join(ARRAY,_s) { for (i=0; i in ARRAY; i++) _s=_s ARRAY[i]; return _s; } # try to be clever and join 2 items at the same time function join2(ARRAY,_s) { for (i=0; i in ARRAY; i+=2) _s=sprintf("%s%s%s",_s,ARRAY[i],ARRAY[i+1]); return _s; } I for one definitely miss having a join() function, and it seems odd that this natural complement to the split() function was never implemented...