Jump to content




[Lua] [Question] files


  • You cannot reply to this topic
2 replies to this topic

#1 mibac138

  • Members
  • 132 posts
  • LocationPoland

Posted 21 June 2013 - 02:55 PM

Hello, is it a method to check how much example file has lines?

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 June 2013 - 03:19 PM

No, there is no function provided to be able to provide the line count in a file.

You will need to do one of the following two things:

— Load each line of the file into a table, and get the count of the table
— Use pattern matching to count the new line characters in the contents of the file

Any more suggestions, or any more specific help, will require you to be a bit less vague and tell us why you want to count the lines in a file, only then can a better answer be given.

#3 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 21 June 2013 - 08:20 PM

Adding some code to BIT's suggestions:

Loading into a table, then counting the entries (sort of, not really - just as easy):
local f = io.open("file", "r")
local lines = 0
while f:read("*l") do lines = lines + 1 end
f:close()

print(lines)

Pattern matching:
local f = io.open("file", "r")
local a = f:read("*a")

-- The quickest way I could think of off the top of my head :P/>/>
local _, lines = a:gsub("\n", "")

print(lines)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users