ThomasDickey / original-mawk

bug-reports for mawk (originally on GoogleCode)
http://invisible-island.net/mawk/mawk.html
18 stars 2 forks source link

Array creation #60

Closed ghost closed 5 years ago

ghost commented 5 years ago

AWK should have a variadic function for array creation. Currently if you wish to create an array, you must use this:

dd[1] = "aa"
dd[2] = "bb"
dd[3] = "cc"

Or:

split("aa bb cc", dd)

the split syntax is problematic if your elements contain spaces. That can be worked around by using a custom separator:

split("aa bb\1cc", dd, "\1")

but then it will fail again if your separator happens to be part of one of the elements. Many other languages have syntax for array literals, for example C:

char *dd[] = {"aa", "bb", "cc"};

Python:

dd = ['aa', 'bb', 'cc']

JavaScript:

var dd = ['aa', 'bb', 'cc'];

Ruby:

dd = ['aa', 'bb', 'cc']

Go:

dd := []string {"aa", "bb", "cc"}

with AWK, it could look like one of these:

anew(dd, "aa", "bb", "cc")
dd[2] == "bb"
aset(dd, "aa", "bb", "cc")
dd[2] == "bb"
ThomasDickey commented 5 years ago

I'll wait to see how your proposal on Austin works out.