|
| 03 Jan 2013 07:10 AM |
I have a String: "10_33" How would I turn it into, x = 10 y = 33
The String may also be 1 Digit: "2_15" |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:18 AM |
Uhm, maybe I am understanding wrong, but do you mean like do this?
x = 10 y = 33 print(x.."_"..y)
output would be. 10_33 |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:21 AM |
The other way round. I have "10_33" and I want to store them into 2 separate variables x,y. |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:23 AM |
a="8173_506"
x=string.sub(a,1,string.find(a,'_')-1) a=string.sub(a,string.find(a,'_')+1,#a) y=a print(x,y) |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:25 AM |
If you have more than two numbers, just repeat what I did to x like this:
a="8173_506_82364"
x=string.sub(a,1,string.find(a,'_')-1) a=string.sub(a,string.find(a,'_')+1,#a) y=string.sub(a,1,string.find(a,'_')-1) a=string.sub(a,string.find(a,'_')+1,#a) z=a print(x,y,z) |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:25 AM |
| Oh, well Robert beat me to it. I will simply re-state, what he already stated. You would use string.sub() |
|
|
| Report Abuse |
|
|
| |
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 03 Jan 2013 07:31 AM |
You can also use gmatch. Here's a function I made that will be useful to you,
function DivideString(String,Div) local val = {} for v in(String..Div):gmatch("(.-)"..Div)do table.insert(val,v)end return val end
for n,o in pairs(DivideString("810_40","_"))do print(n,o) end
> 1 810 > 2 40 |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:37 AM |
| I can honestly say I've never seen anyone use a function in a for loop that returns a table. Clever. |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 03 Jan 2013 07:39 AM |
| I do it all the time with many things. :P |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:40 AM |
| That so called for loop, is considered a iterator function. ;) Just a heads up. lol |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 07:40 AM |
| Still a for loop either way, it makes me wonder about roblox's wiki. >.> |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 03 Jan 2013 07:42 AM |
| Oh, and also, :GetChildren() is a type of function that is a method, and is commonly used in for loops. |
|
|
| Report Abuse |
|
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 03 Jan 2013 08:27 AM |
AAAAAAARGH what is this. String.find on THESE uses!? PLEASE!
function Divide(s,pattern) return s:gmatch(pattern) end
local t = {}
for match in Divide("10_100", "%d+") do table.insert(t,match) end
x = t[1] y = t[2]
print(x,y)
--OUTPUT: 10 100 |
|
|
| Report Abuse |
|
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 03 Jan 2013 08:28 AM |
| I'm blind today, I looked over another post using gmatch, sorry :/ |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2013 08:34 AM |
| That isn't what he was asking for, he didn't want to get a script that makes a table out of a string, and then returns variables set from the table values... He just wanted to separate the string into different values. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 03 Jan 2013 08:38 AM |
string = "1_24" x,y = string.match("(%d+)_(%d+)")
Should work, but I just recently started checking out string patterns. |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 03 Jan 2013 08:39 AM |
| Jobro's does turn the string into separate values. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 03 Jan 2013 08:41 AM |
| Bewt mine is more efficient QQ |
|
|
| Report Abuse |
|
|
1Topcop
|
  |
| Joined: 09 Jun 2009 |
| Total Posts: 6635 |
|
|
| 03 Jan 2013 08:44 AM |
| But mine returns all values, and it doesn't even have to be a number. c: |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 03 Jan 2013 08:49 AM |
--Only numbers s = "1_24" x,y = s:match("(%d+)_(%d+)")
print(x) print(y)
--Any character
s = "1a_2g4" x,y = s:match("(.+)_(.+)")
print(x) print(y)
|
|
|
| Report Abuse |
|
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 03 Jan 2013 09:35 AM |
| @Robertoman: That's what happens. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
| |
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 03 Jan 2013 09:40 AM |
| Yours only thinks of "2 numbers" mine can also handle more numbres, it only doesnt set them. I can go around with setfenv() tough and a variadic function. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 03 Jan 2013 09:41 AM |
| But since when there are more than 3 coordinates? |
|
|
| Report Abuse |
|
|