bug-bash
[Top][All Lists]
Advanced

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

feature request array expansion of keys values pairs and for k v loop


From: Léa Gris
Subject: feature request array expansion of keys values pairs and for k v loop
Date: Thu, 19 Aug 2021 02:30:24 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Many time I see use case for expanding both keys and values of an array, and every time it requires a loop.

Typical use case is with the dialog command. But there are other use cases where having both key and value expanded would save a loop.

Example:

array=([1]=apple [3]=banana [2]=orange)
for k in "${!array[@]}"; do
  v="${array[k]}"
  printf '%s %s ' "$k" "$v"
done
printf \\n

I'd like some syntax to expand both keys and values into a single scalar. Something like a at sign or another symbol meaning both are expanded:

# Expand key values pairs as distinct arguments
printf '%s ' "${@array[@]}"
printf \\n

or

# Expand key values pairs as IFS joined string
printf %s\\n "${@array[*]}"

Consequently it could allow expanding the for loop with:
for k v in "${@array[@]}"; do
  printf 'Key=%s\tValue=%s\n' "$k" "$v"
done

Although the for loop for this specific case would not be needed as it could be expanded in one go as:

printf 'Key=%s\tValue=%s\n' "${@array[@]}"

But I figure there are other use case where iterating key and value would be a QOL over indexing and assigning the value with statement within the loop.

Obviously it would fit equally well with associative arrays.

--
Léa Gris




reply via email to

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