5 ms·
Author here. As I wrote in the README, this uses GAWK instead of plain AWK. Conveniences of GAWK over AWK in a nutshell: - functions - several additional funct
by rethab 6y ago
Author here.
As I wrote in the README, this uses GAWK instead of plain AWK. Conveniences of GAWK over AWK in a nutshell:
- functions
- several additional functions (eg. bit shifting)
But even GAWK lacks some things that are very common in other languages:
- no variable scope: imagine a calling another function in a for loop and the other function again running a for loop. if both loops use 'i' as the counter, good luck. the workaround for this is to declare the local variables as parameters that are not passed (and separate them with four spaces)
- cannot return array from a function. the workaround is to use pass-by-reference (not sure if the precise definition is applicable here)
- arrays cannot be assigned to another variable. workaround is to loop over array and assign it value by value.
If anybody knows better workarounds, please let me know :)
- ufo 6y agoHonestly, maybe the most robust workaround is to rewrite it in another language... Lua might be a good candidate: it has a similar syntax and also uses 1-based arrays.
- wahern 6y agoBut awk is usually part of the default system install, unlike Lua. And while GNU awk isn't as common, it's still far more likely to be available than Lua, especially on Linux systems. (Though, per elsethread, it shouldn't be too difficult to port to POSIX awk, or the superset of "portable" awk.)
- mzs 6y agoNice project! It's only very old awks that don't support functions (like BSD awk does and nawk does on SunOS but /usr/bin/awk doesn't) and the GNU docs have an implementation of strtonum: https://www.gnu.org/software/gawk/manual/html_node/Strtonum-Function.html https://www.gnu.org/software/gawk/manual/html_node/Strtonum-...
- wahern 6y agoPOSIX awk supports user-defined functions. I'm surprised Solaris' default awk doesn't, but /usr/xpg4/bin/awk does support user-defined functions. (I just confirmed both behaviors on Solaris 11.4.)
- wahern 6y agoIt might be useful to provide an example using POSIX od(1) instead of hexdump(1). hexdump is actually a BSD utility. The Debian Linux version is ported from FreeBSD, while the Red Hat version is a pale imitation provided as a wrapper, IIRC, around the od implementation.[1] `od -An -tu1 -v` should suffice. [1] I wrote a single-file hexdump implementation so know far too much about it: https://25thandclement.com/~william/projects/hexdump.c.html https://25thandclement.com/~william/projects/hexdump.c.html
- deleted 6y ago[deleted]
- flukus 6y ago> the workaround for this is to declare the local variables as parameters that are not passed (and separate them with four spaces) - cannot return array from a function. Does anyone know the history of this? It sounds like something that's to crazy to have been designed and came about as a hack that got widely adopted. It would be nice if they added a local keyword or a prefix or something.
- userbinator 6y agoI thought from the if/else chain in the mainloop that it appears to lack a switch() statement too, but the GAWK documentation indicates that it does have one.