Arrays and Tables
Parallel Arrays
Count Matching Rows
Two arrays can model a tiny table when each position belongs to the same row. A loop can scan the rows and count matches.
Program
Play the script to choose a status filter and see how many rows match it.
parallel_array_filter.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
ids=("A1" "B2" "C3")
states=("ready" "blocked" "ready")
status_filter="ready"
matches=0
for i in "${!ids[@]}"; do
if [[ "${states[$i]}" == "$status_filter" ]]; then
matches=$((matches + 1))
fi
done
echo "$status_filter:$matches"
#!/usr/bin/env bash
ids=("A1" "B2" "C3")
states=("ready" "blocked" "ready")
status_filter="blocked"
matches=0
for i in "${!ids[@]}"; do
if [[ "${states[$i]}" == "$status_filter" ]]; then
matches=$((matches + 1))
fi
done
echo "$status_filter:$matches"
ids ← A1, B2, C3
3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")values this stepA1, B2, C3idsstates ← ready, blocked, ready
3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")5status_filter="ready"values this stepready, blocked, readystatesstatus_filter ← ready
4states=("ready" "blocked" "ready")5status_filter="ready"6matches=0values this stepreadystatus_filtermatches ← 0
5status_filter="ready"6matches=07for i in "${!ids[@]}"; dovalues this step0matchesmatches ← 1
8if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))10fivalues this step0 → 1matches0ireadystates[$i]readystatus_filterif [[ "${states[$i]}" == "$status_filter" ]]; then
7for i in "${!ids[@]}"; do8 if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))values this step1iblockedstates[$i]readystatus_filter1matchesmatches ← 2
8if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))10fivalues this step1 → 2matches2ireadystates[$i]readystatus_filterecho "$status_filter:$matches"
11done12echo "$status_filter:$matches"outputready:2values this stepreadystatus_filter2matches
ids ← A1, B2, C3
3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")values this stepA1, B2, C3idsstates ← ready, blocked, ready
3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")5status_filter="blocked"values this stepready, blocked, readystatesstatus_filter ← blocked
4states=("ready" "blocked" "ready")5status_filter="blocked"6matches=0values this stepblockedstatus_filtermatches ← 0
5status_filter="blocked"6matches=07for i in "${!ids[@]}"; dovalues this step0matchesif [[ "${states[$i]}" == "$status_filter" ]]; then
7for i in "${!ids[@]}"; do8 if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))values this step0ireadystates[$i]blockedstatus_filter0matchesmatches ← 1
8if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))10fivalues this step0 → 1matches1iblockedstates[$i]blockedstatus_filterif [[ "${states[$i]}" == "$status_filter" ]]; then
7for i in "${!ids[@]}"; do8 if [[ "${states[$i]}" == "$status_filter" ]]; then9 matches=$((matches + 1))values this step2ireadystates[$i]blockedstatus_filter1matchesecho "$status_filter:$matches"
11done12echo "$status_filter:$matches"outputblocked:1values this stepblockedstatus_filter1matches
parallel arrays
Parallel arrays use the same index to connect fields from the same row.
array indexes
`${!ids[@]}` expands to the indexes present in the array.
row filter
The loop compares each row's status with the selected filter.