David's Blog

TIL: awk array iteration and sort ordering

I've been doing the first few days of this year's advent of code with awk (GNU awk to be precise). The first day I learned some new things about arrays with awk.

The first was that you can set a global variable called PROCINFO["sorted_in"] to determine the order that arrays (which are really associative arrays not "vectors", I believe) are iterated over for for-loops. There's a bunch of options for sorting array values by string or numerically or by type and the options have ascending and descending variants. One caveat is that since this is controlled by a global variable, you will change the default for later loops unless you preserve the current value and then restore it. The docs are very good.

The second piece was that the array sorting functions (asort and asorti) can also be provided with the same options to adjust sort ordering (e.g. to treat array values as numbers instead of strings).

That second bit was what I needed for my AOC solution on day 1 but both of these are good to know.