Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: uninvited members of associative array



On Fri, Dec 16, 2022 at 12:48 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> I just noticed something.  My big array has ten named elements but if I print it:
>
> printf  "%-20s %s\n" ${(kv)main} > /dev/pts/2

I just want to point out that in general you need to quote the array
expansion here or you'll get surprises if the array has empty keys
and/or values. Consider this:

    typeset -A main=(
      foo '1'
      bar '2'
      baz ''
      qux ''
    )

    printf  '%-4s = %s\n' ${(kv)main}

And the output:

    foo  = 1
    baz  = bar
    2    = qux

Notice that the output is wrong. Quotes to the rescue:

    printf  '%-4s = %s\n' "${(@kv)main}"

Now it works:

    foo  = 1
    baz  =
    bar  = 2
    qux  =

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author