bug-bash
[Top][All Lists]
Advanced

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

Re: @K transformation


From: Léa Gris
Subject: Re: @K transformation
Date: Thu, 19 Aug 2021 12:37:02 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 19/08/2021 à 12:09, Koichi Murase écrivait :

$ printf '<%s>\n' "${A[@]@A}"
<declare>
<-A>
<A=([adsf]="456 789" ["foo bar"]="123 456" )>

The problem of ${A[@]@A} is that it is not so useful when one wants to
define a clone associative array with a different name but with the
same contents as A. Instead, using ${A[@]@K}, one could do

$ declare -A "B=(${A[@]@K})"

to clone the associative array. It is even possible to save the
contents of an associative array in an indexed array as

$ declare -a "saved=(${A[@]@K})"

Hmm..., but for this case, one could actually simply store the
contents in a scalar:

$ saved="${A[*]@K}"


Current implementation of @K is pretty-much useless for the dialog use-case:

#!/usr/bin/env bash

declare -A assoc=(
  [P]=piano
  [TB]='foldable table'
  ['CH AIR']=chair
)

options=("${assoc[@]@K}")

#typeset -p options
#exit

choice="$(
  dialog \
    --backtitle "Test" \
    --title "Test" \
    --menu "Test" \
    0 0 4 \
    "${options[@]}" \
    2>&1 >/dev/tty
)"

Inline with "${array[@]}" expanding as multiple arguments!

"${assoc[@]@K}" shall have expanded into arguments and not expand quotes as string content.

This would have made the @K expansion usable for dialog.

Currently the associative array's key values pairs need be added to the options array in a for loop as:

options=()

for k in "${!assoc[@]}"; do
  options+=("$k" "${assoc[$k]}")
done


--
Léa Gris




reply via email to

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