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
declare -Ais required before the first assignment.- Read with
${tpm[python]}; count with${#tpm[@]}. "${!tpm[@]}"iterates the keys.
Keywords and builtins used here
declaredodoneechoforin
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.
Step 2 of 3 in Arrays, step 11 of 26 in Language basics.