bug-bash
[Top][All Lists]
Advanced

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

Working with array keys (was Re: Parameter Expansion: Case Modification:


From: Dan Douglas
Subject: Working with array keys (was Re: Parameter Expansion: Case Modification: ${var~} not documented)
Date: Tue, 18 Aug 2015 17:31:41 -0500

On Tue, Aug 18, 2015 at 3:50 PM, Chet Ramey <chet.ramey@case.edu> wrote:
> On 8/18/15 1:43 PM, Dan Douglas wrote:
>> On Tuesday, August 18, 2015 9:54:55 AM CDT Isaac Good wrote:
>>> Would you mind sharing the rational behind having it undocumented?
>>
>> Since I like guessing: the syntax for parameter expansion operators is
>> currently non-extensible, so the namespace of terse operators is in limited
>> supply. New syntax should be extensible to suit future needs while keeping 
>> the
>> language minimal. This is new syntax that adds one function that will be
>> rarely used. I can think of better ways to use that operator.
>
> This is true, and I would prefer to not set the ~ `operator' in stone until
> I'm more satisfied with how it works.
>
> Bash-4.4 has the ${param@operator} family of expansions (inspired by a
> similar feature in mksh) as the extensible syntax you're asking for.
>

Ah yeah. I saw the var@Q already. That will be nice!

> I assume you mean the difference betweeen ${!param[@]/followed/bysomething}
> and ${!param[@]}.

Yeah that sort of thing. E.g. a recent example where my best answer is still
pretty bad even with fancy bash 4.3-isms. It would be quite nice to get rid of
the intermediary `keys` array just to look up the keys of keys.

~ $ bash <<\EOF
function rev {
    typeset -n _ref=$1 _ret=$2
    typeset -a keys=("${!_ref[@]}")
    typeset key=0

    for ((; key <= ${#keys[@]}; key++)); do
        _ret[${keys[key - 1]}]=${_ref[${keys[-key]}]}
    done
}

typeset -a a=([0]=a [1]=b [3]=c) b
rev a b
typeset -p b
EOF

declare -a b='([0]="c" [1]="b" [3]="a")'

Thats maybe a little tricky. What doesn't work now is  ${!_ref[@]:(-(key)):1}



reply via email to

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