Keep only values that pass a condition.

filter values Filtering visits every element but only records the values that match the predicate.

Filter Values

limit
filter_values.lua
Replay: real traced execution (multi-file project)
local limit = 4
local output = ""
local count = 0

for _, value in ipairs({2, 4, 6, 8}) do
  if value >= limit then
    count = count + 1
    if output ~= "" then
      output = output .. ","
    end
    output = output .. value
  end
end

print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
local limit = 3
local output = ""
local count = 0

for _, value in ipairs({2, 4, 6, 8}) do
  if value >= limit then
    count = count + 1
    if output ~= "" then
      output = output .. ","
    end
    output = output .. value
  end
end

print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
local limit = 6
local output = ""
local count = 0

for _, value in ipairs({2, 4, 6, 8}) do
  if value >= limit then
    count = count + 1
    if output ~= "" then
      output = output .. ","
    end
    output = output .. value
  end
end

print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
  1. limit ← 4, output ← (empty), count ← 0

    1local limit→ 4 = 4 --@limit=3, 62local output→ (empty) = ""3local count→ 0 = 0
  2. _, value in ipairs({2, 4, 6, 8}) do

    pass 1 of 4
    5for _1, value2 in ipairs({2, 4, 6, 8}) do6  if value >= limit then7    count = count + 1
    All 4 passes — pass 1 is the card above
    pass_valueoutput
    112
    224
    3364 4,
    4484,6 4,6,
  3. count ← 1, output ← 4

    pass 1 of 3
    5for _, value in ipairs({2, 4, 6, 8}) do6  if value4 >= limit4 then7    count→ 1 = count + 18    if output ~= "" then9      output = output .. ","10    end11    output→ 4 = output .. value412  end
    All 3 passes — pass 1 is the card above
    passvaluecountoutput
    140 1(empty) 4
    261 24 4,
    382 34,6 4,6,
  4. output ← 4,

    pass 1 of 2
    7count = count + 18if output4 ~= "" then9  output→ 4, = output .. ","10end
  5. output ← 4,6

    10  end11  output→ 4,6 = output .. value612end
  6. output ← 4,6,

    pass 2 of 2
    7count = count + 18if output4,6 ~= "" then9  output→ 4,6, = output .. ","10end
  7. output ← 4,6,8

    10  end11  output→ 4,6,8 = output .. value812end
  8. print("limit=" .. limit)

    15print("limit=" .. limit4)16print("count=" .. count3)17print("output=" .. output4,6,8)
    outputlimit=4
    count=3
    output=4,6,8
  1. limit ← 3, output ← (empty), count ← 0

    1local limit→ 3 = 32local output→ (empty) = ""3local count→ 0 = 0
  2. _, value in ipairs({2, 4, 6, 8}) do

    pass 1 of 4
    5for _1, value2 in ipairs({2, 4, 6, 8}) do6  if value >= limit then7    count = count + 1
    All 4 passes — pass 1 is the card above
    pass_valueoutput
    112
    224
    3364 4,
    4484,6 4,6,
  3. count ← 1, output ← 4

    pass 1 of 3
    5for _, value in ipairs({2, 4, 6, 8}) do6  if value4 >= limit3 then7    count→ 1 = count + 18    if output ~= "" then9      output = output .. ","10    end11    output→ 4 = output .. value412  end
    All 3 passes — pass 1 is the card above
    passvaluecountoutput
    140 1(empty) 4
    261 24 4,
    382 34,6 4,6,
  4. output ← 4,

    pass 1 of 2
    7count = count + 18if output4 ~= "" then9  output→ 4, = output .. ","10end
  5. output ← 4,6

    10  end11  output→ 4,6 = output .. value612end
  6. output ← 4,6,

    pass 2 of 2
    7count = count + 18if output4,6 ~= "" then9  output→ 4,6, = output .. ","10end
  7. output ← 4,6,8

    10  end11  output→ 4,6,8 = output .. value812end
  8. print("limit=" .. limit)

    15print("limit=" .. limit3)16print("count=" .. count3)17print("output=" .. output4,6,8)
    outputlimit=3
    count=3
    output=4,6,8
  1. limit ← 6, output ← (empty), count ← 0

    1local limit→ 6 = 62local output→ (empty) = ""3local count→ 0 = 0
  2. _, value in ipairs({2, 4, 6, 8}) do

    pass 1 of 4
    5for _1, value2 in ipairs({2, 4, 6, 8}) do6  if value >= limit then7    count = count + 1
    All 4 passes — pass 1 is the card above
    pass_valuelimitcountoutput
    112
    224
    33660 1(empty) 6
    44861 26 6,
  3. count ← 1, output ← 6

    pass 1 of 2
    5for _, value in ipairs({2, 4, 6, 8}) do6  if value6 >= limit6 then7    count→ 1 = count + 18    if output ~= "" then9      output = output .. ","10    end11    output→ 6 = output .. value612  end
  4. count ← 2

    pass 2 of 2
    5for _, value in ipairs({2, 4, 6, 8}) do6  if value8 >= limit6 then7    count→ 2 = count + 18    if output ~= "" then
  5. output ← 6,

    7count = count + 18if output6 ~= "" then9  output→ 6, = output .. ","10end
  6. output ← 6,8

    10  end11  output→ 6,8 = output .. value812end
  7. print("limit=" .. limit)

    15print("limit=" .. limit6)16print("count=" .. count2)17print("output=" .. output6,8)
    outputlimit=6
    count=2
    output=6,8