site-josuah

/usr/josuah
Log | Files | Refs

commit 38b7dcd83980c8b187977d75353bd64873af5f4e
parent 9137196a137e1d58e7abd3116e80dd05e103c38b
Author: Josuah Demangeon <me@josuah.net>
Date:   Thu, 25 Jun 2020 21:45:34 +0200

fix portability issues and improve code

Diffstat:
Mwiki/awk/index.md | 34+++++++++++++++-------------------
1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/wiki/awk/index.md b/wiki/awk/index.md @@ -190,7 +190,6 @@ function sort(array, beg, end) { if (beg >= end) # end recursion return - a = beg + 1 # 1st is the pivot, so +1 b = end while (a < b) { @@ -200,10 +199,8 @@ function sort(array, beg, end) b-- swap(array, a, b) # found 2 misplaced } - if (array[beg] > array[a]) # put the pivot back swap(array, beg, a) - sort(array, beg, a - 1) # sort lower half sort(array, a, end) # sort higher half } @@ -247,11 +244,16 @@ With C and many other languages, there are convenient concise syntax such as `{ "a", "b", "c", ...}`to fill an array with values. An well-known way is: ``` -array["len"] = split("a b c ...", array, " ") +split("a b c ...", array, " ") ``` -Then `array["len"]` contains the length (`length(array)` is unportable), -`array[1]` the first element `"a"`, `array[2]` the second, etc. +Note that this does not saves the length out of split, but in practice I +realized I rarely need it: + +``` +for (i = 1; i in split; i++) + print(i, array[i]) +``` A fold_line() function ----------------------- @@ -353,23 +355,23 @@ command: function localtime(sec, tm, tz, h, m) { - if (!TZOFFSET) { + if (!TZ) { "date +%z" | getline tz close("date +%z") h = substr(tz, 2, 2) m = substr(tz, 4, 2) - TZOFFSET = substr(tz, 1, 1) (h * 3600 + m * 60) + TZ = substr(tz, 1, 1) (h * 3600 + m * 60) } - return gmtime(sec + TZOFFSET, tm) + return gmtime(sec + TZ, tm) } ``` Note that date(1) will only be called the first time localtime() is called, and -the TZOFFSET global variable will be used for the next calls. +the TZ global variable will be used for the next calls. -A mktime() function +A timegm() function ------------------- -Complementary function to gmtime is mktime for converting a `tm[]` array back +Complementary function to gmtime is timegm for converting a `tm[]` array back to an integer representation. This is useful for parsing time values back to an unix timestamp: @@ -384,22 +386,17 @@ function mdays(mon, year) return (mon == 2) ? (28 + isleap(year)) : (30 + (mon + (mon > 7)) % 2) } -function mktime(tm, +function timegm(tm, sec, mon, day) { sec = tm["sec"] + tm["min"] * 60 + tm["hour"] * 3600 - day = tm["mday"] - 1 - for (mon = tm["mon"] - 1; mon > 0; mon--) day = day + mdays(mon, tm["year"]) - - # constants: x * 365 + x / 400 - x / 100 + x / 4 day = day + int(tm["year"] / 400) * 146097 day = day + int(tm["year"] % 400 / 100) * 36524 day = day + int(tm["year"] % 100 / 4) * 1461 day = day + int(tm["year"] % 4 / 1) * 365 - return sec + (day - 719527) * 86400 } ``` @@ -438,7 +435,6 @@ Then a global `OUI` array does the MAC addresss to vendor name mapping: BEGIN { if (oui_table("/var/tmp/oui.txt") < 0) exit(1) - print(OUI[toupper("84a991")]) } ```