You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
4.8 KiB

4 years ago
= Associative array !
4 years ago
Apostolos rootApostolos@swarmlab.io
// Metadata:
:description: Intro and Install
:keywords: Linux, apt
:data-uri:
:toc: right
:toc-title: Πίνακας περιεχομένων
:toclevels: 4
:source-highlighter: highlight
:icons: font
:sectnums:
{empty} +
.NOTE
[NOTE]
====
Assuming you're already logged in
====
== Associative array, dictionary or map
=== Exercise
- vim testfile.sh
- copy-paste
- chmod +x ./testfile.sh
- ./testfile.sh
[source,bash]
----
#!/bin/bash
bash --version # Must be at least version 4 to have associative arrays
declare -A array=(
[1,0]="first name"
[1,1]="second name"
[2,0]="third name"
[3,0]="foo bar"
[test 1]="test bar"
)
var=$(cat <<EOF
declare -A array=(
[1,0]="first name"
[1,1]="second name"
[2,0]="third name"
[3,0]="foo bar"
[test 1]="test bar"
)
EOF
)
echo "$var"
echo ' count with ${#array[@]} result:'
echo ${#array[@]}
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
for with "${!array[@]}"
----------------------------------------------------------
for i in "${!array[@]}"; do
echo "$i => ${array[$i]}";
done
"result: "
EOF
for i in "${!array[@]}"; do
echo "$i => ${array[$i]}";
done
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
for with ${!array[@]}
----------------------------------------------------------
for i in ${array[@]}; do
echo "$i => ${array[$i]}";
done
"result: "
EOF
for i in ${array[@]}; do
echo "$i => ${array[$i]}";
done
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
for with "${!array[*]}"
----------------------------------------------------------
for i in "${!array[*]}"; do
echo "$i => ${array[$i]}";
done
"result: "
EOF
for i in "${!array[*]}"; do
echo "$i => ${array[$i]}";
done
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
for with ${!array[*]}
----------------------------------------------------------
for i in ${!array[*]}; do
echo "$i => ${array[$i]}";
done
"result: "
EOF
for i in ${!array[*]}; do
echo "$i => ${array[$i]}";
done
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
we can get the value of the '1,0' key as array
----------------------------------------------------------
declare -a names=(${array[1,0]})
EOF
declare -a names=(${array[1,0]})
echo ""
echo ""
cat <<EOF
Variable names now contains array:
echo \$names
result: $names
echo \${names[1]}
result: ${names[1]}
EOF
echo ' count with ${#names[@]} result:'
echo ${#names[@]}
echo ""
echo ""
cat <<'EOF'
----------------------------------------------------------
Testing whether a value is missing from an associative array
----------------------------------------------------------
if [ ${array[1,0]+_} ]; then
echo "Found";
else
echo "Not found";
fi
EOF
if [ ${array[1,0]+_} ]; then
echo "Found";
else
echo "Not found";
fi
echo ""
echo ""
echo "----------------------------------------------------------"
echo "indexing"
echo "----------------------------------------------------------"
echo 'KEYS=("${!array[@]}") # Make a normal array containing all the keys in the associative array'
KEYS=("${!array[@]}")
echo ""
echo 'echo ${KEYS[0]} # Find a key via an index'
echo ${KEYS[0]}
echo ""
echo 'echo ${array[${KEYS[0]}]} # Find a value via an index'
echo ${array[${KEYS[0]}]}
echo ""
echo "----------------------------------------------------------"
echo "remove key"
echo "----------------------------------------------------------"
echo ""
echo 'unset array["test 1"] # You must quote keys containing spaces when you unset in an associative array'
unset array["test 1"]
echo ""
echo ' count with ${#array[@]} result:'
echo ${#array[@]}
echo ""
echo "----------------------------------------------------------"
echo ' remove all with "unset array" '
echo "----------------------------------------------------------"
unset array
echo ' count with ${#array[@]} result:'
echo ${#array[@]}
----
=== create
[source,bash]
----
declare -A array=(
[1,0]="first name"
[1,1]="second name"
[2,0]="third name"
[3,0]="foo bar"
[test 1]="test bar"
)
----
=== for
[source,bash]
----
for i in "${!array[@]}"; do
echo "$i => ${array[$i]}";
done
----
=== create from array
[source,bash]
----
declare -a names=(${array[1,0]})
----
=== test
[source,bash]
----
if [ ${array[1,0]+_} ]; then
echo "Found";
else
echo "Not found";
fi
----
=== indexing
[source,bash]
----
KEYS=("${!array[@]}")
echo ${array[${KEYS[0]}]}
----
=== unset key
[source,bash]
----
unset array["test 1"]
----
=== unset all
[source,bash]
----
unset array
----