Jump to content




help extract specified text from long output

computer help lua

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

#1 kain184

  • Members
  • 72 posts

Posted 05 April 2015 - 02:04 AM

hello i am trying to find out if this is possible i have a command i am using on the new command computers. not sure if it is a mod adding this command or vanilla but its a gps command it gives out a long string of info as follows {success player name found at x y z } is there a way to get just the player name and gps data from that output. the command is commands.exec("gps @p") which might be added with forge essentials

#2 KingofGamesYami

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

Posted 05 April 2015 - 03:57 AM

Could you provide an exact result? This is possible to do with patterns, but they are very picky. For example, are there commas between x, y, and z? If so, are there spaces? Does it return decimals? I could go on, but it's much easier if you simply post the output of the command. As in run it, copy/paste result here.

If you wish to attempt this yourself, you'll want to use the string.match function, which is well documented. You'll also need to understand patterns to an extent, which can be a little hard to understand.

#3 kain184

  • Members
  • 72 posts

Posted 05 April 2015 - 06:50 AM

commands.exec("gps @p")
returns
{
"SUCCESS: username is at x, y, z in dim 0 with gamemode creative",
}

#4 valithor

  • Members
  • 1,053 posts

Posted 05 April 2015 - 07:41 AM

Just showing this is possible without using actual lua patterns or string.match.

text = commands.exec("gps @p")
player = text:sub(10, ({text:find(" is at")})[1]-1)
x = text:sub(({text:find(" is at ")})[2]+1,({text:find(",")})[1]-1)
text = text:sub(({text:find(",")})[1]+2,#text)
y = text:sub(1,({text:find(",")})[1]-1)
text = text:sub(({text:find(",")})[1]+2,#text)
z = text:sub(1,({text:find(" in dim ")})[1]-1)

As a side note patterns are probably much much better than this... lol. I was just bored and felt like throwing this together.

Edited by valithor, 05 April 2015 - 07:46 AM.


#5 KingofGamesYami

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

Posted 05 April 2015 - 12:30 PM

local str = "SUCCESS: username is at x, y, z in dim 0 with gamemode creative"
local user, x, y, z = str:match( "SUCCESS: (%S+) is at (%d+), (%d+), (%d+)" )
note: This will break if x, y, z are decimal values.

Edited by KingofGamesYami, 05 April 2015 - 12:30 PM.


#6 kain184

  • Members
  • 72 posts

Posted 05 April 2015 - 03:34 PM

so far as i have seen it only gives whole values

#7 kain184

  • Members
  • 72 posts

Posted 05 April 2015 - 03:55 PM

found out it returns two values the true/false then the table so the code looks like so
bul,str=commands.exec("gps @p")
local user, x, y, z = textutils.serialize(str):match( "SUCCESS: (%S+) is at (%d+), (%d+), (%d+)" )
and it works like a charm now to use it in a table to track users and there gps so long as it is a new user or there gps positions have changed

Edited by kain184, 05 April 2015 - 03:56 PM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users