bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Array literal


From: Wolfgang Laun
Subject: Re: [bug-gawk] Array literal
Date: Sat, 25 May 2019 06:39:22 +0200

It's not quite so simple. In expression based languages such as C and awk,
an assignment is defined to return a value. That's why there is (strictly
speaking) no array _literal_ in C, just an array (and structure)
initializer. You cannot write

   x = {1,2,3}[i];   /* not valid in C */
   y = {"a", "b", "c"};

I do know languages where such a construct (even with arbitrary expressions
in the array constructor) is possible, but this neither fits awk nor the
current maintainer's work load.

If anything, a built-in function
    aset( <variable>, <expression>... )
with the obvious semantics might be much easier to implement.

-W


On Sat, 25 May 2019 at 05:38, Steven Penny <address@hidden> wrote:

> Awk should have syntax for array literals. 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"}
>
> and so on.
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]