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

This reading is not working as hoped

Started by makerimages, 12 November 2012 - 03:10 AM
makerimages #1
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?
Kingdaro #2
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
Watcher7 #3
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 )
makerimages #4
Posted 12 November 2012 - 04:48 AM
THANKS, will try tomorrow, or ASAP
Lyqyd #5
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
makerimages #6
Posted 13 November 2012 - 02:44 AM
WORKED ! THANKS

used
Kingdaro-s code
makerimages #7
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??
Lyqyd #8
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.
makerimages #9
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
remiX #10
Posted 03 December 2012 - 12:58 AM
Change

for i=1, 5 do
to

for i=#fileData, #fileData-4 do
makerimages #11
Posted 03 December 2012 - 01:15 AM
now i dont see a thing
ChunLing #12
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.
makerimages #13
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
Kingdaro #14
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.
ChunLing #15
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.
remiX #16
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
ChunLing #17
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.