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: Stephane Chazelas
Subject: Re: [Help-bash] Why bash does not recoganize array type smartly?
Date: Wed, 16 Mar 2016 08:38:59 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2016-03-16 01:12:18 +0000, Dan Douglas:
[...]
> > zsh's syntax to define an associative array as a whole is a lot
> > saner:
> >
> > typeset -A a
> > a=(
> >   key1 value1
> >   key2 value2
> > )
> >
> > That means, you can easily define associative arrays based on
> > the output of some command, or copy one array to another or
> > merge two arrays:
> >
> > copy_of_a=("${(@kv)a}")
> >
> > merge_of_a_and_b=("${(@kv)a}" "${(@kv)b}")
> >
> > or
> >
> > a+=("${(@kv)b}")
> >
> > Note that since 5.1, zsh supports
> >
> > typeset -A a=(k1 v1 k2 v2)
> 
> Oh I see. That's kind of nice but unintuitive.

The hash=(k v k2 v2)

syntax is intuitive to perl programmers at least. It's also the
most compact one, an important thing considering zsh is before
all an interactive command line interpreter (where code can be
(and usually is) write-only as long as it's easy/short to type).

See the discussion when that feature was introduced in 1998:
http://www.zsh.org/mla/workers/1998/msg00919.html
One good thing with zsh is that the discussion about its
features is public...

While we're at discussing quirks, note that bash can't have the
empty string as a hash key (!?). AFAIK, zsh is the only shell that
can have arbitrary sequences of bytes (including 0) as both key
and value.

It's difficult to set a hash element with an empty key
with arbitrary bytes with the hash[key]=value syntax in zsh
though. 

Like

a[${}]=1 # for empty key
# or k= a[$k]=1
k=$'\u20ac\0\x80'
a[$k]=2

Easier to use the:

a+=('' 1 $'\u20ac\0\x80' 2)

in that case.

> Namerefs also provide
> some simple ways to accomplish those same things that are less
> difficult to use portably IMO.

AFAIK, namerefs are not copies.


> If we're being honest, neither of these are particularly "sane". A
> sane language just lets you zip two lists together and cast to the
> desired mapping type, or call whatever methods are supported by a
> particular collection interface. e.g. LINQ
> 
>  $ csharp -pkg:dotnet -e '
> new[] { "foo", "bar", "baz", "bleh", "Blerg" }
>     .Select((x, num) => new KeyValuePair<string, int>(x, num))
>     .ToDictionary(x => x.Key, x => x.Value);'
> {{ "foo", 0 }, { "bar", 1 }, { "baz", 2 }, { "bleh", 3 }, { "Blerg", 4 }}
> 
> Or the dynamic langs of course have equivalents...
> `python -c 'print(dict(enumerate(["foo", "bar", "baz", "bleh",
> "Blerg"])))'` etc.
[...]

There's no way I'm going to type anything like that at a shell
prompt.

I'm fine with

typeset -A a
a=(x qwe y asd)
for k (${(k)a) something with $k $a[$k]

Or

for k v (${(kv)a}) something with $k $v

(the aboves assume non-empty key/values, it's more cumbersome if
there may be like for k v ("${(@kv)a}") something with "$k"
"$v". That empty removal is one of the choices made by zsh I
don't like).

-- 
Stephane




reply via email to

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