0

I have a string which delimited by \n, and I'm using for to do something with that. But I don't want the for loop to the end of the string, just want the for loop to any times like this code below but it doesn't work.

for w=1,10 in webdata:gmatch("(.-)\n") do 
    --something
end

1 Answer 1

2

There are two different for loops in Lua. You are mixing them together.

Numerical for:

for i=1,10 do -- or e.g. i=10,1,-1
   -- do something
end

Generic for:

for k,v in pairs(t) do -- or ipairs, or completely custom functions
   -- do something
end

For more information please refer to:

Your problem

To achieve your goal you could wrap gmatch with another iterator or... just go with the most straight-forward and simple solution: count the lines you have processed:

local n = 1
for l in webdata:gmatch("(.-)\n") do
   -- do something
   n = n + 1
   if n > 10 then
      break
   end
end

It's not the most elegant one, but it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.