3 ms·
Funny how exploiting uninitialized variables is evil in (nonstrict) JavaScript but good in Awk. I agree that’s true, mind you, I just can’t pinpoint what about
by mananaysiempre 2y ago
Funny how exploiting uninitialized variables is evil in (nonstrict) JavaScript but good in Awk. I agree that’s true, mind you, I just can’t pinpoint what about the languages’ designs makes it work in one case but not the other.
- rusk 2y agoGuaranteed initialisation rules I would imagine. A lot of languages don’t do this, or assign null or something. For a language with a narrow scope of application you can this but in more general purpose languages it makes less sense.
- mananaysiempre 2y agoNote Awk does have a special value for uninitialized variables—its distinguishing feature is that it compares equal to both zero and empty string simultaneously. (No official way to spell that value, but you can spell it e.g. “undefined” if you never initialize a variable called “undefined”.) It’s really really close to old JS in that respect, and no wonder, given that Awk was an explicit inspiration for JS. But somehow it works for Awk and not for JS, even at the hundred-line scale that’s common and reasonable for both languages.
- rusk 2y agoI never considered that awk was a precursor to JavaScript but I can certainly see that
- mananaysiempre 2y agoFound it! Wirfs-Brock & Eich, “JavaScript: the first 20 years” (HOPL ’20) [1], §3.1: > The syntax of JavaScript 1.0 was directly modeled after the statement syntax of the C programming language with some AWK inspired embellishments. (the rest of the section is also interesting in that respect). [1] https://dl.acm.org/doi/10.1145/3386327 https://dl.acm.org/doi/10.1145/3386327 (CC-BY open access)
- rusk 2y agoI could feel it all along!
- hi41 2y agoDoesn’t awk set null for all uninitialized variables. Does that solve the problem for you? We can use that feature to find lines with same values which form a group.
- asicsp 2y agoNot sure if it was intended, but it helps to keep CLI one-liners concise. For example: awk '!a[$0]++' awk '/search/{n=2} n && n--'
- AdieuToLogic 2y ago> Funny how exploiting uninitialized variables is evil in (nonstrict) JavaScript but good in Awk. I agree that’s true, mind you, I just can’t pinpoint what about the languages’ designs makes it work in one case but not the other. I believe Awk's semantic behaviour is a kindred spirit of Perl's autovivication[0]. It is often handy and sometimes can bite one in the proverbial backside. 0 - https://en.wikipedia.org/wiki/Autovivification https://en.wikipedia.org/wiki/Autovivification
- theamk 2y agoA lot of things are good in language designed for one-liners, but will be considered evil in languages which are used for longer programs. For an opposite example, consider: a 1000 statement JS program is perfectly fine. A 1000 statement AWK script is very evil, most people would be saying it should be replaced with real language ASAP.
- rakoo 2y agoI think it's ok in awk because awk is seen as a scripting language, where brevity matters and there aren't many variables anyway
- 7thaccount 2y agoAwk is meant for short scripts (often one liners) that get some piped input. A JS program could be used in that manner, but I'd guess it'd be more like an exception than the norm. Different tools for different needs.