[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 13:54:19 +0000 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2016-03-16 01:12:18 +0000, Dan Douglas:
[...]
> $ 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 }}
[...]
For the record, what the equivalent could be in zsh:
a=(foo bar baz bleh Blerg)
indices=({1..$#a})
typeset -A h=(${a:^indices})
With bash, you probably need a loop:
a=(foo bar baz bleh Blerg)
typeset -A h
for i in "address@hidden"; do h[${a[i]}]=$i; done
Though here, assuming I'd be needing something like that, I'd
probably go for a loop in zsh as well as that would seem more
natural to me than that zipping operator:
for i ({1..$#a}) h[$a[i]]=$i
or
n=0; for i ($a) h[$i]=$((++n))
>
> Or the dynamic langs of course have equivalents...
> `python -c 'print(dict(enumerate(["foo", "bar", "baz", "bleh",
> "Blerg"])))'` etc.
h=(${indices:^a})
> I don't think it would be too much of a strech for ksh to add
> interfaces and iterators. It needs to clean up its type system a bit
> and expose more of the shell's functionality through it.
>
> Bash could do the same with some extra syntax to represent glib
> collections it could do a lot without having to re-invent the wheel.
The zipping operator ${array1:^array2} is relatively recent in
zsh.
It may reinvent the wheel, but here it's a wheel fitted to doing
day to day admin tasks efficiently at a shell prompt while the
C#/python/glib you refer to are more programming languages.
--
Stephane
- Re: [Help-bash] Why bash does not recoganize array type smartly?, (continued)
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Chet Ramey, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Dan Douglas, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Stephane Chazelas, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Dan Douglas, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Greg Wooledge, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Dan Douglas, 2016/03/16
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Stephane Chazelas, 2016/03/17
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Chet Ramey, 2016/03/17
- Re: [Help-bash] Why bash does not recoganize array type smartly?,
Stephane Chazelas <=
- Re: [Help-bash] Why bash does not recoganize array type smartly?, Dan Douglas, 2016/03/16