How to find the length of a text file
Started by kylergs, Apr 16 2012 03:50 PM
14 replies to this topic
#1
Posted 16 April 2012 - 03: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.
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.
#2
Posted 16 April 2012 - 03:55 PM
kylergs, on 16 April 2012 - 03:50 PM, said:
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.
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)
#3
Posted 16 April 2012 - 04:32 PM
I meant to find the length of a text file or program.
#5
Posted 16 April 2012 - 05:39 PM
I don't know what to look for.
I can't find anything obvious.
I can't find anything obvious.
#7
Posted 16 April 2012 - 06:09 PM
Sorry, the number of lines in a text or program file. God I'm bad at this
#8
Posted 16 April 2012 - 06: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)
#9
Posted 16 April 2012 - 08:45 PM
Open the file and capture it in a sting then #(lua length operator) the string..
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.
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 by luanub, 16 April 2012 - 08:49 PM.
#10
Posted 16 April 2012 - 08: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.
As has been said before, I'd recommend checking the edit source code.
#11
Posted 17 April 2012 - 03:46 PM
well thats how u get it in a table
/>
and that is how u read the lenght
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)
#12
Posted 17 April 2012 - 04: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:
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.
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)
#13
Posted 18 April 2012 - 08: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 { }
#14
Posted 19 April 2012 - 06:21 PM
I use Notepad++ for the coding, it supports quite alot languages. But just remember to save it as a FILE.
#15
Posted 19 April 2012 - 09:14 PM
Doing something like local var = {} declares the var as a table.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











