I agree with him. His book "The Design and Evolution of C++" was a fascinating read. His site has an overview of it.
$ awk 'function f() { a[1]=2; return a } BEGIN { f() }' </dev/null
awk: cmd. line:1: fatal: attempt to use array `a' in a scalar context
You also can't have nested associative arrays, i.e. recursive or cyclic data structures are not allowed.As far as I can tell, this is because a stack frame owns everything allocated within it, and when a function returns the whole stack frame is cleaned up unconditionally. You can pass arrays down but not up. It's very naive memory management (by modern standards; it's probably better than BASIC).
So I'd say it's clearly not expressive enough for general purpose programming. There are Lisps in awk but they do weird tricks with text as far as I remember.
The advantage of awk over python is that it almost always takes < 60 seconds from the point at which you say, "I want to summarize, review, filters, report" on some columnar data to the point at which you have output in front of you.
I"m fast with python doing that - but it's usually a 3-4 minute set of steps.
The only exception being json, which awk is clumsy with. (jq is my goto tool for the simple queries, python for anything that requires more than a minute of thought.)
Also - it's almost always the case that I'm using awk in conjunction with a bunch of other tools, cut/sed/uniq/grep to get what I want.
The problem of persisting arrays usually leads to handling them in bash.
Now you have two problems.[1]
[1] If you paraphrase Jamie Zawinski in an explanation, you then have two things to explain.
And I still think Shell, Awk, and Make Should be Combined. In that world you'd still have one problem :)
https://www.oilshell.org/blog/tags.html?tag=awk#awk
Oil has recursive data structures, although I'm still working out how to compose shell-like "procs" and functions which could return an associative array. We could just add them both naively but I think it would cause many shell programs to be a lot messier.
- they don't have any garbage collection or ref counting
- they don't have recursive data structures
- you can't return any kind of array from a function
In bash you also can't pass an array to a function, but in Awk you can.Not quite -- https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/... was the first and used two global associative arrays, car[n] and cdr[n], where n is an integer which iirc had to have a particular tag modulo 4, as the type tag. (Much as you'd do it in a lower-level C implementation.) I used almost the same scheme in my later Lisp.
A bit of pdf2txt, awk and some shell scripting, and budgeting becomes very easy...
But ok, we can at least say that awk is the oldest programming language to have associative arrays that's still in widespread use. (Yes, I'm hoping that someone will argue for SNOBOL4's current widespread use. :) )
> However, from the standpoint of manipulating strings with regular expressions, AWK introduced the concept as far as I know.
Two can play at this game! `awk` is really a successor of `sed`, which is all about manipulating strings with regular expressions. sed maybe isn't turing complete though (but someone's gonna prove me wrong here too) or at any rate not convenient to use in as general a way as awk.
Done! :) Since a turing machine has been written in sed, sed is turing complete. [1] [2] (both from Peter of Browserling).
[1] https://catonmat.net/proof-that-sed-is-turing-complete
[2] https://catonmat.net/ftp/sed/turing.txt> Christophe isn't the first person to realize that sed is almost a general purpose programming language. People have written tetris, sokoban and many other programs in sed.
Wow! But clearly, you wouldn't WANT to except for the fun of it.