typestar

Associative arrays in Bash

String keys via declare -A.

# declare -A makes keys strings instead of indexes
declare -A tpm
tpm[bash]=85
tpm[python]=104
tpm[rust]=88

echo "python pace: ${tpm[python]}"
echo "languages tracked: ${#tpm[@]}"

for lang in "${!tpm[@]}"; do
    echo "$lang -> ${tpm[$lang]}"
done

How it works

  1. declare -A is required before the first assignment.
  2. Read with ${tpm[python]}; count with ${#tpm[@]}.
  3. "${!tpm[@]}" iterates the keys.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
245
Tokens
60
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 48 seconds.

Type this snippet

Step 2 of 3 in Arrays, step 11 of 26 in Language basics.

← Previous Next →