Arrays

Concepts:
Bash arrays and associative arrays.

Text:
Write an example that illustrates the use of bash arrays and associative arrays.
In bash array, the index of the array must be an integer number. Some gaps may be present, i.e., indices can be not continuous.
Instead, bash associative arrays are like hash maps, and the index of the array must be a string.

Solution:

arrays.sh
#!/bin/bash
# Example of use of the BASH arrays (and associative arrays)
 
# Classical array
echo "Classical array"
vett2[3]=pasta
vett2[6]=bread
echo ${vett2[3]}  # Prints pasta
echo ${#vett2[*]} # Prints 2
echo ${vett2[*]}  # Prints bread pasta
echo ${!vett2[*]} # Prints 3 6
 
 
# Associative array
echo -e "\nAssociative array"
declare -A vett1
 
vett1[stefano]=3
nome=giulia
vett1[$nome]=pippo
 
echo ${vett1[stefano]}  # Prints 3
echo ${vett1["giulia"]} # Prints pippo
echo ${#vett1[*]}       # Prints 2
echo ${vett1[*]}        # Prints pippo 3
echo ${!vett1[*]}       # Prints giulia stefano
 
sum=0
for name in ${!vett1[*]}
do
    let sum=${vett1[$name]}+$sum
done
echo "Sum: $sum"
 
unset vett1
echo ${#vett1[*]} # Prints 0 (because vett1 does not exist)

Output:

$ ./arrays.sh
Classical array
pasta
2
pasta bread
3 6
 
Associative array
3
pippo
2
pippo 3
giulia stefano
Sum: 3
0