Skip to content

asa

asa

collection of function to work on associative array

Note

unstable

L_asa_copy

Copy associative dictionary.

Notice: the destination array is not cleared. Slowish, O(N). Iterates of keys one by one

Arguments:

  • $1 var Source associative array
  • $2 var Destination associative array
  • [$3] str Filter only keys with this regex

See: L_asa_copy

L_asa_has

check if associative array has key

Arguments:

  • $1 associative array nameref
  • $2 key

L_asa_is_empty

check if associative array is empty

Argument: $1 associative array nameref

L_asa_get

Get value from associative array

Option: -v <var> var

Arguments:

  • $1 associative array nameref
  • $2 key
  • [$3] optional default value

Exit: 1 if no key found and no default value

L_asa_get_v

L_asa_len

get the length of associative array

Option: -v <var> var

Argument: $1 associative array nameref

L_asa_len_v

L_asa_keys_sorted

get keys of an associative array in a sorted

Option: -v <var> var

Argument: $1 associative array nameref

L_asa_keys_sorted_v

L_asa_set

assign value to associative array

You might think why this function exists? In case you have associative array name in a variable.

Example

   local -A map
   printf -v "map[a]" "%s" val  # will fail in bash 4.0
   L_asa_set map a val  # will work in bash4.0

Arguments:

  • $1 <var> assoatiative array variable
  • $2 <str> key to assign to
  • $3 <str> value to assign

L_asa_from_declare

Extract associative array from string

Example

   declare -A map=([a]=b [c]=d)
   declare string=""
   string=$(declare -p "map")
   declare -A mapcopy=()
   L_asa_from_declare mapcopy = "${string}"

Arguments:

  • $1 var associative array nameref to store
  • $2 =
  • $3 var source variable nameref

See:

L_asa_assign

Copy associative dictionary

Notice: the destination array is cleared. Much faster then L_asa_copy. Note: Arguments are in different order.

Example

  local -A map=([a]=b [c]=d)
  local -A mapcopy=()
  L_asa_assign mapcopy = map

Arguments:

  • $1 var Destination associative array
  • $2 =
  • $3 var Source associative array

See: