| View previous topic :: View next topic |
| Author |
Message |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Thu Jun 15, 2006 4:51 pm Post subject: |
|
|
| Altair wrote: | Yes what you did is the same as what i did.
I didn't know the command tonumber existed. Thx for that! |
That's what I thought, saves us a lot of code!
But if I want to send an x and an y (and more), what would be the best way to do this?
make 1 long string of, and cut it in pieces at the other psp?
or send them seperatly? But how does the other psp know which is the x and y? |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Fri Jun 16, 2006 1:53 am Post subject: |
|
|
| Hmm yeah, i guess still use one big string and then cut it into pieces. So it still doesnt help to much, the "tonumber" command. |
|
| Back to top |
|
 |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Fri Jun 16, 2006 2:50 am Post subject: |
|
|
| Altair wrote: | | Hmm yeah, i guess still use one big string and then cut it into pieces. So it still doesnt help to much, the "tonumber" command. |
maybe be2003 knows how to solve this in a smooth way too... |
|
| Back to top |
|
 |
SSpeare
Joined: 23 May 2006 Posts: 63
|
Posted: Fri Jun 16, 2006 3:32 am Post subject: |
|
|
You could build a string like this:
| Code: |
-- Assuming:
x = 45
y = 56
-- Do this:
send = x.."^"..y
-- Now send = "45^56"
|
And then parse it like this:
| Code: | x = tonumber(piece(data,"^",1))
y = tonumber(piece(data,"^",2)) |
If you use a function "piece" like this:
| Code: |
function piece(str,delim,num)
if num < 1 then
return ""
end
local lastEnd = 0
local matchBegin,matchEnd = string.find(str,delim,1,true)
local loop = 1
for i=2,num do
loop = i
lastEnd = matchEnd
matchBegin,matchEnd = string.find(str,delim,matchEnd+1,true)
if (matchBegin == nil) then
matchBegin = string.len(str)+1
break
end
end
if num > loop then
return ""
end
return string.sub(str,lastEnd+1,matchBegin-1)
end
|
You could use anything instead of "^", that's just an example. And you can put more data in there. As much as you want. The piece function is kind of wasteful if you are getting lots of data out, but it works. If you want it to be more efficient you could use a Tokenizer pattern or something.
Last edited by SSpeare on Fri Jun 16, 2006 7:18 am; edited 1 time in total |
|
| Back to top |
|
 |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Fri Jun 16, 2006 5:30 am Post subject: |
|
|
there is a lot of new code for me in that, but I will look into it later, maybe tomorrow, got some stuff to do now...
But thanx for the help! |
|
| Back to top |
|
 |
be2003
Joined: 20 Apr 2006 Posts: 144
|
Posted: Fri Jun 16, 2006 7:21 am Post subject: |
|
|
... sending coordinates...
first define ur x and y
x = 2
y = 5
then make em one string separated by a comma
coXY = tostring(x)..","..tostring(y)
send it
System.irdaWrite(coXY)
on the receiving end
oppcoXY = System.irdaRead()
blah blah blah, just receive the string like normal ok
make a fuction to turn it back
function parseXY(oppcoXY)
for i=1, string.len(oppcoXY) do
char = string.sub(oppcoXY,i,i)
sy = ""
sx = ""
tempxory = "x"
if char == "," then
tempxory = "y"
elseif tempxory == "x" then
sx = sx..char
else tempxory == "y" then
sy = sy..char
end
end
oppx = tonumber(sx)
oppy = tonumber(sy)
end
this function takes i the received string and writes the vars oppx and oppy to the ram and can be accessed anytime if they arent changed to something else. _________________ - be2003
blog |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Fri Jun 16, 2006 10:37 am Post subject: |
|
|
| Ok thx. So its basicly what we already did but then with the addition of tonumber() (which makes stuff easier though) |
|
| Back to top |
|
 |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Sat Jun 17, 2006 3:18 am Post subject: |
|
|
| Altair wrote: | | Ok thx. So its basicly what we already did but then with the addition of tonumber() (which makes stuff easier though) |
yeah, pretty kewl community out here, there really helpfull, thanx to anyone who helped us out! |
|
| Back to top |
|
 |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Sat Jun 17, 2006 3:40 am Post subject: |
|
|
| Quote: |
if char == "," then
tempxory = "y"
elseif tempxory == "x" then
sx = sx..char
else tempxory == "y" then
sy = sy..char
end
|
shouldnt it be an "elseif" or an "=" instead of an "else" or "=="???
Last edited by Kakolukia on Sat Jun 17, 2006 7:16 am; edited 1 time in total |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Sat Jun 17, 2006 4:08 am Post subject: |
|
|
| yes it has to be "elseif" |
|
| Back to top |
|
 |
Kakolukia
Joined: 11 Jun 2006 Posts: 21
|
Posted: Sat Jun 17, 2006 4:26 am Post subject: |
|
|
| figured it out, = gives an error, but you probably tried it too, and found out ;) |
|
| Back to top |
|
 |
be2003
Joined: 20 Apr 2006 Posts: 144
|
Posted: Mon Jun 19, 2006 2:30 pm Post subject: oops |
|
|
the function should work if you define sx, sy, and tempxory before the whole "for i=..." or else everytime the for command makes a cycle then the vars get reset. sorry _________________ - be2003
blog |
|
| Back to top |
|
 |
|