Arrays and Dictionaries Flashcards

1
Q

How do you define the entire array automatically using a file?

A

readarray -t myArr < sample.txt

echo ${myArr[0]} #Index 0 of the array will contain the first line of sample.txt

echo ${myArr[1]} #Index 1 of the array will contain the second line of sample

More ways: https://linuxopsys.com/bash-readarray-with-examples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you define the entire array explicitly like assigning a variable?

A

Fruits=(‘Apple’ ‘Banana’ ‘Orange’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Printing array entries?

A

echo “Method 1:”
for i in ${myArr[@]}
do
echo $i
done

echo “Method 2:”
for (( i=0; i<${#myArr[@]}; i++ ))
do
echo ${myArr[$i]}
done

Method 3:
echo “${Fruits[@]}”
all elements; space separated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Print a single entry in an array:

A

echo ${myArr[0]}

where 0 is the first entry

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Printing the last element in an array

A

echo “${Fruits[-1]}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

print the number of elements

A

echo “${#Fruits[@]}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

print the
string length of the 1st
element

A

echo “${#Fruits}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

print the
string length of the nth
element

A

echo “${#Fruits[3]}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

print the range from position 3, length 2

A

echo “${Fruits[@]:3:2}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Keys of all elements, space-separated

A

echo “${!Fruits[@]}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Array Operations

A

Fruits=(“${Fruits[@]}” “Watermelon”) # Push
Fruits+=(‘Watermelon’) # Also Push
Fruits=( “${Fruits[@]/Ap*/}” ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=(“${Fruits[@]}”) # Duplicate
Fruits=(“${Fruits[@]}” “${Veggies[@]}”) # Concatenate
lines=(cat "logfile") # Read from file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Defining an array as a dictionary

A

declare -A sounds

declares “sounds” as a dictionary object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a
dictionary (associative arrays)
differ from a normal array?

A

Arrays are numerical starting with element 0,1, … N

Dictionaries aka associative arrays have a keys/values association instead of numerical/elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give an example of a dictionary (associative array)

A

sounds[dog]=”bark”
sounds[cow]=”moo”
sounds[bird]=”tweet”
sounds[wolf]=”howl”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Example use of calling one Dictionary entry:

A

set entry:
sounds[dog]=”bark”

example call:
echo “${sounds[dog]}”

would print:
bark

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Print all dictionary values (answers)

A

echo “${sounds[@]}”

17
Q

Print all dictionary keys (element values)

A

echo “${!sounds[@]}”

18
Q

Give the number of elements in a dictionary

A

echo “${#sounds[@]}”

19
Q

delete the entry dog in this array:
declare -A sounds
sounds[dog]=”bark”

A

unset sounds[dog]

20
Q

Print dictionary elements by iterating over values

A

for VALUE in “${sounds[@]}”; do
echo “${VALUE}”
done

21
Q

Print dictionary elements by iterating over keys

A

for KEY in “${!sounds[@]}”; do
echo “${KEY}”
done