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

How to get the line count for a program

Started by houseofkraft, 24 September 2016 - 01:18 PM
houseofkraft #1
Posted 24 September 2016 - 03:18 PM
Hi guys!

I want to make it count how much lines are in a program

It would be useful because i am making a wireless printer and i would like to know how much lines it printed

Thanks!

EDIT: I need it to know how much lines are in the program before it prints so it can say like "Printing… 1/40"
Edited on 24 September 2016 - 01:27 PM
Lupus590 #2
Posted 24 September 2016 - 08:20 PM
readLine into a table, get length of table

I have a feeling that this is a bad way to do it though, lots of reads makes slow program

you may be able to read all and count the newline characters, you'll need to know patterns for that: https://www.lua.org/pil/20.2.html
Edited on 24 September 2016 - 06:21 PM
KingofGamesYami #3
Posted 24 September 2016 - 09:29 PM
I don't think Lupus's solution would work - I'm pretty sure printing word-wraps, making the number of newline characters unreliable. You would also have to know the maximum number of characters per line, and how many times it has to wrap.
Edited on 24 September 2016 - 07:29 PM
Sewbacca #4
Posted 24 September 2016 - 09:42 PM
Easiest way:

local nLines = #io.lines 'FilePath'
or

nLines = 0
for _ in io.lines 'FilePath' do -- typing mistake, sorry
  nLines = nLines + 1
end
(both untested)

One of them should work.
Edited on 25 September 2016 - 10:35 AM
Lupus590 #5
Posted 24 September 2016 - 09:59 PM
Easiest version:

local nLines = #io.lines 'FilePath'
or

nLines = 0
for _ in #io.lines 'FilePath' do
  nLines = nLines + 1
end
(all untested)

One of them have to work.
still doesn't account Yami's post
Lyqyd #6
Posted 24 September 2016 - 11:51 PM
And that's really, really not how io.lines works. Please don't post suggestions that "have to work" without trying them first.
Bomb Bloke #7
Posted 24 September 2016 - 11:58 PM
I'm pretty sure printing word-wraps

To my memory, and according to how it's documented, it works the same as term.write(). That means no automatic word-wrap, and it also means that if you fail to manually move the cursor to the newline position when you're about to go off the current page… then the cursor will simply go off the page, and it won't be possible to read what's written there.

With cPrint I rigged things so that you could treat the virtual printer as a terminal, meaning you can redirect to it and use print (which, in turn, handles word-wrap and all that good stuff for you). Catch is cPrint is a bit of a cheat - it doesn't work with "real" printers. It does make things a lot easier though - you can print in multiple colours without having to mess around with inks, and you can print to multiple pages without having to mess around with paper. To make something similar that works with a proper printer, you'd need a turtle stocked with dyes.

Of course, this is all putting aside that printing a script probably isn't going to produce anything useful? You can't scan the printout back into anything. If you want to back a script up, you're much better off using pastebin for the purpose.
Edited on 24 September 2016 - 10:07 PM
Sewbacca #8
Posted 25 September 2016 - 10:33 AM
I don't think Lupus's solution would work - I'm pretty sure printing word-wraps, making the number of newline characters unreliable. You would also have to know the maximum number of characters per line, and how many times it has to wrap.
Do you mean that it has to create a newline after the line is bigger than the width of the paper?
And that's really, really not how io.lines works. Please don't post suggestions that "have to work" without trying them first.
I was pretty sure that one of the solutions would work.
Just give it a try. You may also take a look at this.
still doesn't account Yami's post
I just thought it might have been a better way of counting lines.
Edited on 25 September 2016 - 10:56 AM
H4X0RZ #9
Posted 25 September 2016 - 11:47 AM
I don't think Lupus's solution would work - I'm pretty sure printing word-wraps, making the number of newline characters unreliable. You would also have to know the maximum number of characters per line, and how many times it has to wrap.
You mean that it have to create a newline after the line is bigger than the width of the paper?
And that's really, really not how io.lines works. Please don't post suggestions that "have to work" without trying them first.
And that's really, really how io.lines works. Please don't comment posts that "cannot work" without trying them first.
I knew that one of the solutions have to work.
You can test it or just look at this.
still doesn't account Yami's post
I just wanted to post a better way, counting the lines.

Both of your examples are wrong though. The second one is almost right, but the "#" will break it.

Please do your homework before telling a moderator (which I highly respect for their knowledge) what's wrong and what isn't ;D
Sewbacca #10
Posted 25 September 2016 - 11:53 AM
Both of your examples are wrong though. The second one is almost right, but the "#" will break it.

Please do your homework before telling a moderator (which I highly respect for their knowledge) what's wrong and what isn't ;D
Typing mistake, i tested it without #.
Edited on 25 September 2016 - 12:01 PM
Sewbacca #11
Posted 25 September 2016 - 02:01 PM
Please excuse my english. I edited some phrases that sounded rather impolite.
houseofkraft #12
Posted 25 September 2016 - 05:35 PM
Hi Guys!

I made a printing program successfully but it does not make another page when the page is filled with text. So it only prints one page of text no matter what.

How do i detect when the page is filled with text?

Thanks!
KingofGamesYami #13
Posted 25 September 2016 - 05:47 PM
When you'be exceeded the size of the page, you should create a new sheet.
Lyqyd #14
Posted 25 September 2016 - 07:37 PM
Threads merged. Please stick to one thread for all questions about a given program.