Jump to content




How to "read" a line by it pos

help

  • You cannot reply to this topic
14 replies to this topic

#1 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 04 August 2015 - 11:20 PM

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"

#2 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 04 August 2015 - 11:31 PM

string.find
local myString = "This is a string"
local str = myString:find(6,7) --#Can be string.find(myString,6,7)
print(str)
--# "is"


#3 CoderPuppy

  • Members
  • 121 posts

Posted 04 August 2015 - 11:35 PM

View PostDragon53535, on 04 August 2015 - 11:31 PM, said:

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"


#4 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 04 August 2015 - 11:40 PM

Oh thanks , I have already saw the string sub but I not find so much info of it.

#5 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 05: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.

#6 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 05 August 2015 - 05: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 by TheOddByte, 05 August 2015 - 05:25 PM.


#7 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 05: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 by lukasloko, 05 August 2015 - 05:29 PM.


#8 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 05:35 PM

The question is : the function string.sub() know when the word are on onother string or it simple resume counting normaly?

#9 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 05 August 2015 - 05: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 by TheOddByte, 05 August 2015 - 05:43 PM.


#10 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 05: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 by lukasloko, 05 August 2015 - 05:44 PM.


#11 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 07:14 PM

View PostTheOddByte, on 05 August 2015 - 05:42 PM, said:

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?

#12 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 05 August 2015 - 07:23 PM

line:find should be v:find.

#13 flaghacker

  • Members
  • 655 posts

Posted 05 August 2015 - 08: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.

#14 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 05 August 2015 - 08:41 PM

View Postflaghacker, on 05 August 2015 - 08:29 PM, said:

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.

#15 lukasloko

  • Members
  • 28 posts
  • Location<Insert a location joke here>

Posted 05 August 2015 - 10:30 PM

View Postflaghacker, on 05 August 2015 - 08:29 PM, said:

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.

View PostMKlegoman357, on 05 August 2015 - 08:41 PM, said:

View Postflaghacker, on 05 August 2015 - 08:29 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users