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

gsub function

Started by Viproz, 17 November 2012 - 01:12 AM
Viproz #1
Posted 17 November 2012 - 02:12 AM
Hi,

I'm doing a turtle program for minning but i have a trouble with the gsub function…

I want to remove a string in another sting so here is my problem (line 271) :

posDesMinerai = posDesMinerai:gsub(posToBase..","..hauteur..","..largeur..";")

where posToBase, hauteur and largeur are int, posDesMinerai is a string like that "0,1,5;1,2,-1;"

The error is :

minning:271: bad argument :
string/function/table expected

the posDesMinerai var may had already be cut by a gsub.

So please help me :)/>/>

PS : I'm frensh so in the code there is some words in french and oters in English ^^
Fiery Arcanine #2
Posted 17 November 2012 - 02:49 AM
And we need the full code
Viproz #3
Posted 17 November 2012 - 03:19 AM
I will not share the full code (maybe i will but only when it will be finished), i can show you the vars value


posToBase = 0
[...]
posToBase = posToBase + 1

hauteur = 0
[...]
hauteur = hauteur + 1


largeur = 0
[...]
largeur = largeur - 1

posDesMinerai = string.char()
posDesMinerai = posDesMinerai..(posToBase + 1)..","..hauteur..","..largeur..";"

After i split posDesMinerai but it not interfere whit the var just use it.
Fiery Arcanine #4
Posted 17 November 2012 - 03:23 AM
Then I'm sorry.
We can't help unless the full code is given

there is nothing wrong with this part
Doyle3694 #5
Posted 17 November 2012 - 03:24 AM
btw, use
 tags! :)/>/>
GopherAtl #6
Posted 17 November 2012 - 03:26 AM
gsub needs 2 parameters, a pattern to match and a value to replace that pattern with. Example:


local str="foo"
str=str:gsub("o","0")
print(str)
output:

f00

to replace with nothing you need to put an empty string for 2nd param


local str="abc"
str=str:gsub("b","")
print(str)
outputs:

ac
Orwell #7
Posted 17 November 2012 - 03:39 AM
Then I'm sorry.
We can't help unless the full code is given
Speak for yourself. Can't you be a bit more understanding?

there is nothing wrong with this part
How isn't there?

First off, this isn't correct:

posDesMinerai = string.char()
Should this be:

posDesMinerai = string.char(posDesMinerai)
?

Second, I'm not sure where you're going with this, but this line:

posDesMinerai:gsub(posToBase..","..hauteur..","..largeur..";")
Should probably be something along the lines of:

local ptB1,h1,l1,ptB2,h2,l2 = posDesMinerai:gfind("(%d),(%d),(%d);(%d),(%d),(%d)")
Not sure about escaping special characters right now.

If you meant to split "x1,y1,z1;x2,y2,z2" to "x1,y1,z1" and "x2,y2,z2" separately then you could try something like this:

local str1,str2 = posDesMinerai:gsub("(%w);(%w)")
Or something.
Viproz #8
Posted 17 November 2012 - 03:56 AM
gsub needs 2 parameters, a pattern to match and a value to replace that pattern with.

Thanks you very much !
I forget to put the second parameter…
But now the code is working =)


First off, this isn't correct:

posDesMinerai = string.char()
Should this be:

posDesMinerai = string.char(posDesMinerai)
?

Second, I'm not sure where you're going with this, but this line:

posDesMinerai:gsub(posToBase..","..hauteur..","..largeur..";")
Should probably be something along the lines of:

local ptB1,h1,l1,ptB2,h2,l2 = posDesMinerai:gfind("(%d),(%d),(%d);(%d),(%d),(%d)")
Not sure about escaping special characters right now.

If you meant to split "x1,y1,z1;x2,y2,z2" to "x1,y1,z1" and "x2,y2,z2" separately then you could try something like this:

local str1,str2 = posDesMinerai:gsub("(%w);(%w)")
Or something.


posDesMinerai = string.char()

With that i just want to clear the string.

For the split function I've find that on internet :


function mySplit(inputstr, sep)
		if sep == nil then
				sep = "%s"
		end
		t={} ; i=1
		for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
				t[i] = str
				i = i + 1
		end
		return t
end

That's work pretty well !


Thanks all for your help :)/>/>