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.

status_filter
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"
  1. ids ← A1, B2, C3

    3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")
    values this stepA1, B2, C3ids
  2. states ← ready, blocked, ready

    3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")5status_filter="ready"
    values this stepready, blocked, readystates
  3. status_filter ← ready

    4states=("ready" "blocked" "ready")5status_filter="ready"6matches=0
    values this stepreadystatus_filter
  4. matches ← 0

    5status_filter="ready"6matches=07for i in "${!ids[@]}"; do
    values this step0matches
  5. matches ← 1

    8if [[ "${states[$i]}" == "$status_filter" ]]; then9    matches=$((matches + 1))10fi
    values this step0 1matches0ireadystates[$i]readystatus_filter
  6. if [[ "${states[$i]}" == "$status_filter" ]]; then

    7for i in "${!ids[@]}"; do8    if [[ "${states[$i]}" == "$status_filter" ]]; then9        matches=$((matches + 1))
    values this step1iblockedstates[$i]readystatus_filter1matches
  7. matches ← 2

    8if [[ "${states[$i]}" == "$status_filter" ]]; then9    matches=$((matches + 1))10fi
    values this step1 2matches2ireadystates[$i]readystatus_filter
  8. echo "$status_filter:$matches"

    11done12echo "$status_filter:$matches"
    outputready:2
    values this stepreadystatus_filter2matches
  1. ids ← A1, B2, C3

    3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")
    values this stepA1, B2, C3ids
  2. states ← ready, blocked, ready

    3ids=("A1" "B2" "C3")4states=("ready" "blocked" "ready")5status_filter="blocked"
    values this stepready, blocked, readystates
  3. status_filter ← blocked

    4states=("ready" "blocked" "ready")5status_filter="blocked"6matches=0
    values this stepblockedstatus_filter
  4. matches ← 0

    5status_filter="blocked"6matches=07for i in "${!ids[@]}"; do
    values this step0matches
  5. if [[ "${states[$i]}" == "$status_filter" ]]; then

    7for i in "${!ids[@]}"; do8    if [[ "${states[$i]}" == "$status_filter" ]]; then9        matches=$((matches + 1))
    values this step0ireadystates[$i]blockedstatus_filter0matches
  6. matches ← 1

    8if [[ "${states[$i]}" == "$status_filter" ]]; then9    matches=$((matches + 1))10fi
    values this step0 1matches1iblockedstates[$i]blockedstatus_filter
  7. if [[ "${states[$i]}" == "$status_filter" ]]; then

    7for i in "${!ids[@]}"; do8    if [[ "${states[$i]}" == "$status_filter" ]]; then9        matches=$((matches + 1))
    values this step2ireadystates[$i]blockedstatus_filter1matches
  8. echo "$status_filter:$matches"

    11done12echo "$status_filter:$matches"
    outputblocked:1
    values 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.