help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why bash does not recoganize array type smartly?


From: Dan Douglas
Subject: Re: [Help-bash] Why bash does not recoganize array type smartly?
Date: Wed, 16 Mar 2016 13:39:11 +0000

On 3/16/16, Greg Wooledge <address@hidden> wrote:
> It's 100% intuitive coming from Tcl, which uses almost exactly the same
> syntax for (associative) array definitions.
>
> $ tclsh /dev/stdin <<'EOF'
>> array set a {k1 v1 k2 v2}
>> puts $a(k2)
>> EOF
> v2
>
> The list of key/value pairs can be split across lines however you want.
> E.g.:
>
> array set a {
>   k1 v1
>   k2 v2
> }

It's unintuitive because the same assignment does something completely
different depending on the variable type. Bash currently has no way to
test for the type of an array, so a function that manipulates an array
it didn't define itself would become even more unsafe than it already
is.

Also as I mentioned - without a built-in zip/unzip function or any way
to directly get the keys/values at once, you have to write that
function yourself, which requires at least a few loops that negate the
purpose of having alternating keys/values in assignments.

That said, we could technically add this without conflict because
`typeset -A arr=(foo bar)` is currently an invalid assignment. It
would add some theoretical compatibility with zsh at least so I'm not
completely opposed to the idea. I'd still prefer it if bash also had
the address@hidden expansion from ksh93 so that some minimal type checking is
possible.



reply via email to

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