236 posts
Posted 12 November 2012 - 04:10 AM
hi, i have a piece of code:
file = fs.open("log","r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()
local recent = fileData[1], fileData[2], fileData[3], fileData[4], fileData[5]
print("Recent Activitys:"..recent)
what i would like it to to is to read the
5 latest lines from the file and print them like this:
Recent activitys:
line
by
line
by
line
currently it prints like this
Recent activitys:THE FIRST THING ON THE FILE
what should i change?
1688 posts
Location
'MURICA
Posted 12 November 2012 - 04:37 AM
You're printing your lines wrong. You either have to append 'n' characters at the end of each one, or print them all separately. I personally would go the second route.
local recent = fileData[1], fileData[2], fileData[3], fileData[4], fileData[5]
print("Recent Activitys:"..recent)
|
|
V
print('Recent Activities:')
for i=#filedata, #filedata-4, -1 do
if not fileData[i] then break end -- for protection, in case there are less than 5 lines.
print(fileData[i])
end
23 posts
Location
Equestria, TX
Posted 12 November 2012 - 04:44 AM
Change
local recent = fileData[1], fileData[2], fileData[3], fileData[4], fileData[5]
print("Recent Activitys:"..recent)
to
local recent = ( table.concat( fileData, 'n', ( ( #fileData > 5 ) and ( #fileData - 4 ) or 1 ), #fileData ) .. 'n' )
write( "Recent activities:n" .. recent )
236 posts
Posted 12 November 2012 - 04:48 AM
THANKS, will try tomorrow, or ASAP
8543 posts
Posted 12 November 2012 - 06:24 AM
local recent = fileData[1], fileData[2], fileData[3], fileData[4], fileData[5]
print("Recent Activitys:"..recent)
Should be:
print("Recent Activities:")
if #fileData >= 5 then
for i = #fileData - 4, #fileData do
print(fileData[i])
end
elseif #fileData >= 1 then
for i = 1, #fileData do
print(fileData[i])
end
else
print("None!")
end
236 posts
Posted 13 November 2012 - 02:44 AM
WORKED ! THANKS
used
Kingdaro-s code
236 posts
Posted 02 December 2012 - 01:24 AM
so, now i get the lines, but its only showing the first 5 lines, but i want it to read the last 5 lines(eg. the latest added 5 lines). How To??
8543 posts
Posted 02 December 2012 - 05:48 AM
That sounds like what you'd get if you were using your original code, or if you started adding lines to the top of the file instead of the bottom. Please post the full current code.
236 posts
Posted 03 December 2012 - 12:46 AM
file = fs.open("log","r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()
print('Recent Activities:')
for i=1, 5 do
if not fileData[i] then break end -- for protection, in case there are less than 5 lines.
print(fileData[i])
end
this is what I currently have
2088 posts
Location
South Africa
Posted 03 December 2012 - 12:58 AM
Change
for i=1, 5 do
to
for i=#fileData, #fileData-4 do
236 posts
Posted 03 December 2012 - 01:15 AM
now i dont see a thing
2005 posts
Posted 03 December 2012 - 03:57 AM
"for i=#fileData, #fileData-4,-1 do" You need to explicitly set the loop to decrement, the default increment operator is +1 unless otherwise specified, even if the end is lower than the beginning. Skipping the loop entirely is the expected behavior in that case because the start because it is usually the desired behavior.
At least, it's the desired behavior when I write a for loop.
Note, this will print the results in reverse order. If you want them in the other order, use "for i = #fileData - 4, #fileData do", as Lyqyd demonstrated.
236 posts
Posted 03 December 2012 - 05:19 AM
Huh?? Would you mind using a sentence i can undrestand. Because the start because. Is not vey understandable
1688 posts
Location
'MURICA
Posted 03 December 2012 - 06:09 AM
He means that, in a for, the first value is your starting number, the second is your ending number, and the third is how much you add each time.
When you don't give a third number, lua assumes you want to go up by 1 each time. However, when your first number is bigger than your second, lua doesn't do anything, because you'll inevitably end up at infinity and never reach the second number. This is why you have to give a third number, -1, to tell lua to go down and not up.
2005 posts
Posted 03 December 2012 - 09:30 AM
Yes. Well, actually the reason lua doesn't do anything is because the "finish" condition is already reached, using the default incrementation logic. But the reason that behavior is good is because the alternative is an infinite loop (lua doesn't reach infinity, just a value so large that the computer can't add 1 and change it further, because the numbers are represented using floating point this doesn't cause them to "rollover", they just become too large for the computer to properly note the effect of adding 1).
I was going to explain this and then gave up in the middle (or right after the start…literally). I do that sometimes.
Anyway, just put the code the way Kingdaro or Lyqyd posted it. I will work.
2088 posts
Location
South Africa
Posted 03 December 2012 - 10:20 AM
Oh, I thought if the first number is bigger than the second it automatically goes in an increment of -1. Woops
2005 posts
Posted 04 December 2012 - 09:46 AM
No, this behavior is not desired. You can make it so that the increment value is negative, but in most cases if the programmer didn't know at the outset that the condition value would be smaller then the desired behavior would be to skip the loop.
Also, it's much easier on the interpreter to just interpret things that way, cause the conditional logic is dependent on the incrementation value anyway. If you look at how iterative for loops are written in C-like languages, this is more apparent.