This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kylergs's profile picture

How to find the length of a text file

Started by kylergs, 16 April 2012 - 02:50 PM
kylergs #1
Posted 16 April 2012 - 04:50 PM
I was wondering if there was a way to find the length of a text file.
The idea is to be able to scroll though any code/text file and print each line on the screen, I can already scroll though a text document if I know its length but I want it to work with any program.
I am relevantly confident with lua otherwise.
Kolpa #2
Posted 16 April 2012 - 04:55 PM
I was wondering if there was a way to find the length of a text file.
The idea is to be able to scroll though any code/text file and print each line on the screen, I can already scroll though a text document if I know its length but I want it to work with any program.
I am relevantly confident with lua otherwise.




randomstring = "troreolfoifndsofmnwoifinsop"
length = string.len(randomstring)
print(length)
kylergs #3
Posted 16 April 2012 - 05:32 PM
I meant to find the length of a text file or program.
cant_delete_account #4
Posted 16 April 2012 - 05:46 PM
I meant to find the length of a text file or program.
Why don't you take a look at .minecraft/mods/ComputerCraft/lua/rom/programs/edit?
kylergs #5
Posted 16 April 2012 - 06:39 PM
I don't know what to look for.
I can't find anything obvious.
Kolpa #6
Posted 16 April 2012 - 07:05 PM
I meant to find the length of a text file or program.
well then read the text or program file :)/>/>
like this:
local file = io.open("textfile.txt", "r")
local randomstring = file:readAll()
kylergs #7
Posted 16 April 2012 - 07:09 PM
Sorry, the number of lines in a text or program file. God I'm bad at this
kamnxt #8
Posted 16 April 2012 - 07:45 PM
Try this code:

filename = "file"
file = fs.open(filename, "r")
length = 0
while true do
line = file.readLine()
if not line then break else length = length + 1 end
end
file.close()
print(length)
Luanub #9
Posted 16 April 2012 - 09:45 PM
Open the file and capture it in a sting then #(lua length operator) the string..


file = io.open("filename", "r")
sFile = file:read("*a") -- read entire file to var sFile
file:close()
nLength = #sFile -- Captures the length of sFile in nLength
print (nLength)

Wow I need to read more. There is a similar way to do the lines by converting it to a table then running table.maxn() on the table. But kamnxt's code is allot shorter of an option.
Edited on 16 April 2012 - 07:49 PM
Cloudy #10
Posted 16 April 2012 - 09:52 PM
To be honest, the best way would be to be putting each line in a table - that way you can find the length of the table and as such the number of lines. Will also help for scrolling through any line and getting the text of that line.

As has been said before, I'd recommend checking the edit source code.
Kolpa #11
Posted 17 April 2012 - 04:46 PM
well thats how u get it in a table :)/>/>
local tLines = {}
local file = io.open("filename", "r" )
  local sLine = file:read()
  while sLine do
   table.insert( tLines, sLine )
   sLine = file:read()
  end
  file:close()

and that is how u read the lenght
print(#tLines)
OminousPenguin #12
Posted 17 April 2012 - 05:32 PM
I just wanted to point out to less experienced users that the best way to count the lines rather depends on what you want to do with the file, if anything.
If you're not actually interested in the contents of the file, just the number of lines, then the following code is probably best:


local file = io.open("file", "r")
local length = 0
while file:read() ~= nill do
  length = length + 1
end
file:close()
print(length)

Edit: Ok so my interest ran away with me and I tested it. If you don't need the ease of access and editing that tables gives you, then you have the option of these two methods. The one above does not store the contents and so is cheaper on memory, the one bellow is faster. Although for the size of files most people are dealing with neither is a concern.


local file = io.open("file", "r")
local contents = file:read("*a")
file:close()
local _, num = string.gsub(contents,"n","n")
print(num+1)
kylergs #13
Posted 18 April 2012 - 09:43 PM
Thanks for the replies. Also, can you explain some of the less obvious parts of the code, I'd really like to learn how to become more fluent, my only experience with Lua is this mod and I haven't been too into it for that long. For example the use of { }
WirelessDude569 #14
Posted 19 April 2012 - 07:21 PM
I use Notepad++ for the coding, it supports quite alot languages. But just remember to save it as a FILE.
Luanub #15
Posted 19 April 2012 - 10:14 PM
Doing something like local var = {} declares the var as a table.