Build a compact label string from the first records in a stream.

limit The selected limit controls how many labels enter the joined output.
separator The pipeline appends separators only between selected labels.

Join Labels

limit
join_labels.lua
Replay: real traced execution (multi-file project)
local limit = 2
local joined = ""
local count = 0

for _, label in ipairs({"red", "green", "blue"}) do
  if count < limit then
    if joined ~= "" then
      joined = joined .. "|"
    end
    joined = joined .. label
    count = count + 1
  end
end

print("limit=" .. limit)
print("count=" .. count)
print("joined=" .. joined)
local limit = 1
local joined = ""
local count = 0

for _, label in ipairs({"red", "green", "blue"}) do
  if count < limit then
    if joined ~= "" then
      joined = joined .. "|"
    end
    joined = joined .. label
    count = count + 1
  end
end

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

for _, label in ipairs({"red", "green", "blue"}) do
  if count < limit then
    if joined ~= "" then
      joined = joined .. "|"
    end
    joined = joined .. label
    count = count + 1
  end
end

print("limit=" .. limit)
print("count=" .. count)
print("joined=" .. joined)
  1. limit ← 2, joined ← (empty), count ← 0

    1local limit→ 2 = 2 --@limit=1, 32local joined→ (empty) = ""3local count→ 0 = 0
  2. _, label in ipairs({"red", "green", "blue"}) do

    pass 1 of 3
    5for _1, labelred in ipairs({"red", "green", "blue"}) do6  if count < limit then7    if joined ~= "" then
    All 3 passes — pass 1 is the card above
    pass_labellimitjoinedcount
    11red2(empty) red0 1
    22green2red red|1
    33blue
  3. joined ← red, count ← 1

    pass 1 of 2
    5for _, label in ipairs({"red", "green", "blue"}) do6  if count0 < limit2 then7    if joined ~= "" then8      joined = joined .. "|"9    end10    joined→ red = joined .. labelred11    count→ 1 = count + 112  end
  4. if count < limit then

    pass 2 of 2
    5for _, label in ipairs({"red", "green", "blue"}) do6  if count1 < limit2 then7    if joined ~= "" then8      joined = joined .. "|"
  5. joined ← red|

    6if count < limit then7  if joinedred ~= "" then8    joined→ red| = joined .. "|"9  end
  6. joined ← red|green, count ← 2

    9  end10  joined→ red|green = joined .. labelgreen11  count→ 2 = count + 112end
  7. print("limit=" .. limit)

    15print("limit=" .. limit2)16print("count=" .. count2)17print("joined=" .. joinedred|green)
    outputlimit=2
    count=2
    joined=red|green
  1. limit ← 1, joined ← (empty), count ← 0

    1local limit→ 1 = 12local joined→ (empty) = ""3local count→ 0 = 0
  2. _, label in ipairs({"red", "green", "blue"}) do

    pass 1 of 3
    5for _1, labelred in ipairs({"red", "green", "blue"}) do6  if count < limit then7    if joined ~= "" then
    All 3 passes — pass 1 is the card above
    pass_labellimitjoinedcount
    11red1(empty) red0 1
    22green
    33blue
  3. joined ← red, count ← 1

    5for _, label in ipairs({"red", "green", "blue"}) do6  if count0 < limit1 then7    if joined ~= "" then8      joined = joined .. "|"9    end10    joined→ red = joined .. labelred11    count→ 1 = count + 112  end
  4. print("limit=" .. limit)

    15print("limit=" .. limit1)16print("count=" .. count1)17print("joined=" .. joinedred)
    outputlimit=1
    count=1
    joined=red
  1. limit ← 3, joined ← (empty), count ← 0

    1local limit→ 3 = 32local joined→ (empty) = ""3local count→ 0 = 0
  2. _, label in ipairs({"red", "green", "blue"}) do

    pass 1 of 3
    5for _1, labelred in ipairs({"red", "green", "blue"}) do6  if count < limit then7    if joined ~= "" then
    All 3 passes — pass 1 is the card above
    pass_labeljoined
    11red
    22greenred red|
    33bluered|green red|green|
  3. joined ← red, count ← 1

    pass 1 of 3
    5for _, label in ipairs({"red", "green", "blue"}) do6  if count0 < limit3 then7    if joined ~= "" then8      joined = joined .. "|"9    end10    joined→ red = joined .. labelred11    count→ 1 = count + 112  end
    All 3 passes — pass 1 is the card above
    passlabeljoinedcount
    1red(empty) red0 1
    2red red|1
    3red|green red|green|2
  4. joined ← red|

    pass 1 of 2
    6if count < limit then7  if joinedred ~= "" then8    joined→ red| = joined .. "|"9  end
  5. joined ← red|green, count ← 2

    9  end10  joined→ red|green = joined .. labelgreen11  count→ 2 = count + 112end
  6. joined ← red|green|

    pass 2 of 2
    6if count < limit then7  if joinedred|green ~= "" then8    joined→ red|green| = joined .. "|"9  end
  7. joined ← red|green|blue, count ← 3

    9  end10  joined→ red|green|blue = joined .. labelblue11  count→ 3 = count + 112end
  8. print("limit=" .. limit)

    15print("limit=" .. limit3)16print("count=" .. count3)17print("joined=" .. joinedred|green|blue)
    outputlimit=3
    count=3
    joined=red|green|blue