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

How to "read" a line by it pos

Started by lukasloko, 04 August 2015 - 10:20 PM
lukasloko #1
Posted 05 August 2015 - 12:20 AM
Sorry if this is a obvious question but I really don't find on anywhere.
I need to "read" a line by it pos , like this:

str = "why you do that?"

3rd_word = findbypos(9,10)

print(3rd_word)

return:

"do"
Dragon53535 #2
Posted 05 August 2015 - 12:31 AM
string.find

local myString = "This is a string"
local str = myString:find(6,7) --#Can be string.find(myString,6,7)
print(str)
--# "is"
CoderPuppy #3
Posted 05 August 2015 - 12:35 AM
string.find

local myString = "This is a string"
local str = myString:find(6,7) --#Can be string.find(myString,6,7)
print(str)
--# "is"
That except it's sub not find.

local myString = "This is a string"
local str = myString:sub(6,7) --#Can be string.sub(myString,6,7)
print(str)
--# "is"
lukasloko #4
Posted 05 August 2015 - 12:40 AM
Oh thanks , I have already saw the string sub but I not find so much info of it.
lukasloko #5
Posted 05 August 2015 - 06:15 PM
But this will give the exactly position like:

archive1 :
hey i am
a archive
and i have
words on
lines
—————
code:
function that reads the archive1 and prints on the screen
———-
the X will reset to one at each line end?
if you don't understand i can try to explain again.
TheOddByte #6
Posted 05 August 2015 - 06:24 PM
If I understood correctly you're probably looking for something like term.setCursorPos


local x, y = 1, 1

term.clear() --# Clear the screen

local file = fs.open( "<filename>", "r" )
for line in file.readLine do -- #Loop through all the lines in the file
	term.setCursorPos( x, y ) --# Set the cursor position
	term.write( line ) --# Write the line
	y = y + 1
end
If I'm incorrect then please explain further on what you meant
Edited on 05 August 2015 - 04:25 PM
lukasloko #7
Posted 05 August 2015 - 06:29 PM
Ok , I ill try to explain better , I m not American,so if I write somethings wrong , sorry..
Here is:
I making a web browser, and to add a click support , to click on the screen on a text and go to an other link, I need the browser to scan for a especific word, and then see the X and Y pos of it, if you click in the pos you go to the link, and I having problems with this code because the X pos is a little strange of the supposed to be.I think is because the function don't know when the string is on another line.
Edited on 05 August 2015 - 04:29 PM
lukasloko #8
Posted 05 August 2015 - 06:35 PM
The question is : the function string.sub() know when the word are on onother string or it simple resume counting normaly?
TheOddByte #9
Posted 05 August 2015 - 06:42 PM
Hmm.. So you'd probably want todo something like this

--# Store all lines in a table
local lines = {}


--# Get all the lines from the file
local file = fs.open( "<your file>", "r" )
for line in file.readLine do
	table.insert( lines, line )
end


--# This function will loop through all the lines
--# And check if it can find the specific word in any line
--# It will return a table like this: { x1 = 1, x2 = 2, y = 1 }
local function find( word )
	local results = {}
	for i, v in pairs( lines ) do --# Loop through all the lines
		if line:find( word ) then --# Check and see if you can find the specific word in the line
			local x1, x2 = line:find( word ) --# Get the first and last position of the word
			table.insert( results, {
				x1 = x1;
				x2 = x2;
				y  = i;
			})
		end
	end
end

local results = find( "Hello" )
print( textutils.serialize( results ) )
This is just an example, if you understood what I did here then it can probably be helpful to you, the question is, do you want to find a word by it's position? or do you want to search for it as a string( string.find( str, str2 ) )


Edit: Try creating a file and put some random text in it, and put Hello somewhere in it and see if it returns the result you expect it to return
Edited on 05 August 2015 - 04:43 PM
lukasloko #10
Posted 05 August 2015 - 06:43 PM
I want to find the word position, the word is always random, but I have a code after and before it ,that i find the position and i can calcutate where is to the mouse click
Edited on 05 August 2015 - 04:44 PM
lukasloko #11
Posted 05 August 2015 - 08:14 PM
Hmm.. So you'd probably want todo something like this

--# Store all lines in a table
local lines = {}


--# Get all the lines from the file
local file = fs.open( "<your file>", "r" )
for line in file.readLine do
	table.insert( lines, line )
end


--# This function will loop through all the lines
--# And check if it can find the specific word in any line
--# It will return a table like this: { x1 = 1, x2 = 2, y = 1 }
local function find( word )
	local results = {}
	for i, v in pairs( lines ) do --# Loop through all the lines
		if line:find( word ) then --# Check and see if you can find the specific word in the line
			local x1, x2 = line:find( word ) --# Get the first and last position of the word
			table.insert( results, {
				x1 = x1;
				x2 = x2;
				y  = i;
			})
		end
	end
end

local results = find( "Hello" )
print( textutils.serialize( results ) )
This is just an example, if you understood what I did here then it can probably be helpful to you, the question is, do you want to find a word by it's position? or do you want to search for it as a string( string.find( str, str2 ) )


Edit: Try creating a file and put some random text in it, and put Hello somewhere in it and see if it returns the result you expect it to return
Have an error on the line if line:find( word ) then or its me?
KingofGamesYami #12
Posted 05 August 2015 - 08:23 PM
line:find should be v:find.
flaghacker #13
Posted 05 August 2015 - 09:29 PM
Is your question actually "how do I check on what text the user clicked on the screen"? If so, that's impossible. You should keep the coordinates of printed text yourself, and then use the data of the mouse_click event to find out what text was clicked.
MKlegoman357 #14
Posted 05 August 2015 - 09:41 PM
Is your question actually "how do I check on what text the user clicked on the screen"? If so, that's impossible. You should keep the coordinates of printed text yourself, and then use the data of the mouse_click event to find out what text was clicked.

It is possible, but as flaghacker pointed out, it's something that won't work that great. You would actually want a system that's more advanced than this. You could go with how HTML does this (and many other GUI frameworks), by having each object (a button, textbox or simply text) be a separate object. I'm talking about an Object-Oriented Programming (OOP) approach. It's really something that might not be as easy as you might expect, so my suggestion would be to first make some simpler programs, like a door lock with clickable buttons on a monitor. Also, you could look and see how different button APIs handle buttons.
lukasloko #15
Posted 05 August 2015 - 11:30 PM
Is your question actually "how do I check on what text the user clicked on the screen"? If so, that's impossible. You should keep the coordinates of printed text yourself, and then use the data of the mouse_click event to find out what text was clicked.
Is your question actually "how do I check on what text the user clicked on the screen"? If so, that's impossible. You should keep the coordinates of printed text yourself, and then use the data of the mouse_click event to find out what text was clicked.

It is possible, but as flaghacker pointed out, it's something that won't work that great. You would actually want a system that's more advanced than this. You could go with how HTML does this (and many other GUI frameworks), by having each object (a button, textbox or simply text) be a separate object. I'm talking about an Object-Oriented Programming (OOP) approach. It's really something that might not be as easy as you might expect, so my suggestion would be to first make some simpler programs, like a door lock with clickable buttons on a monitor. Also, you could look and see how different button APIs handle buttons.
I am tryng to find a word coordenates and then check if the cursor clicked on it