Here is a Bash model of how, associative array support could be added to mapfile, replacing the need for interpreted while-loop read and logic by a much more efficient improved mapfile built-in:

#!/usr/bin/env bash

kv_cr_stream () {
  printf 'key1=value1\nkey2=value=2\nkey 3=value3\n'
}
kv_null_stream () {
  printf 'key1=value1\0key2=value=2\0key\n3=value3\0'
}

declare -A assoc_cr=()

# Load associative array from line records
# Proposed new feature:
# IFS='=' mapfile -A assoc_cr
while IFS='=' read -r k v || [[ -n $k && -n $v ]]; do
  assoc_cr[$k]=$v
done < <(kv_cr_stream)

declare -p assoc_cr

declare -A assoc_null=()

# Load associative array from null delimited records
# Proposed new feature:
# IFS='=' mapfile -d '' -A assoc_null
while IFS='=' read -r -d '' k v || [[ -n $k && -n $v ]]; do
  assoc_null[$k]=$v
done < <(kv_null_stream)

declare -p assoc_null

Expected output:
declare -A assoc_cr=([key2]="value=2" [key1]="value1" ["key 3"]="value3" )
declare -A assoc_null=([key2]="value=2" [key1]="value1" [$'key\n3']="value3" )




--
Léa Gris


Reply via email to