|
| 06 Oct 2016 01:46 AM |
Simple question, though I asked something similar before.
Let's say I have this string:
"C:2,A:7,S:45,T:29,L:0,E:2"
I want to turn this into five variables.
C,A,S,T,L,E = 2,7,45,29,0,2
What is the correct way to do this? |
|
|
| Report Abuse |
|
|
| |
|
|
| 06 Oct 2016 01:48 AM |
| Not entirely sure if there's a better way of doing this, but use loops and setfenv() or shared. |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:52 AM |
| I can't see how something like that meant for most metatables it looks like is meant to separate this string. A loop maybe but in what fashion to break it apart? In context I want the numbers. |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 02:03 AM |
Why the heck would you use setfenv here?
local String = "C:2,A:7,S:45,T:29,L:0,E:2" for name, digit in String:gmatch("(%a):(%d)") do getfenv()[name] = digit end
print(C) -- 2 |
|
|
| Report Abuse |
|
|
KreoBox
|
  |
| Joined: 24 Aug 2016 |
| Total Posts: 356 |
|
|
| 06 Oct 2016 02:08 AM |
"I can't see how something like that meant for most metatables it looks like is meant to separate this string."
What? Setfenv has nothing to do with metatables.
pastebin /baKWg7L6 |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 08:14 AM |
| Look up string patterns in my help desk I'm about to post. |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 11:12 AM |
The getfenv didn't get all digits so I ended up doing this.
local String = "C:2,A:7,S:45,T:29,L:0,E:2"
_,Community,_,Agility,_,Service,_,Teamwork,_,Leadership,_,Endurance = String:match("([^:]+):(%d+),([^:]+):(%d+),([^:]+):(%d+),([^:]+):(%d+),([^:]+):(%d+),([^:]+):(%d+)")
print(Community,Agility,Service,Teamwork,Leadership,Endurance) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 11:33 AM |
@xsoul u couldve just done
local String = "C:2,A:7,S:45,T:29,L:0,E:2" for name, digit in String:gmatch("(%a+):(%d+)") do getfenv()[name] = digit end
𝒷𝓁𝑜𝒷𝒻𝒾𝓈𝒽 𝒶𝓇𝑒 𝑔𝑜𝒾𝓃𝑔 𝓉𝑜 𝒷𝑒 𝒷𝓇𝒾𝒸𝓀𝓈 𝒻𝑜𝓇 𝓂𝓎 𝓌𝒶𝓁𝓁; 𝓉𝒶𝓇𝒹𝒾𝑔𝓇𝒶𝒹𝑒𝓈 𝒶𝓇𝑒 𝑔𝑜𝒾𝓃𝑔 𝓉𝑜 𝒷𝑒 𝓉𝒽𝑒 𝑔𝓁𝓊𝑒 𝒻𝑜𝓇 𝓂𝓎 𝓌𝒶𝓁𝓁 |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:01 PM |
| I didn't realize you wanted all the digits, mb |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:28 PM |
local String = "C:2,A:7,S:45,T:29,L:0,E:2" for Variable, Value in String:gmatch("(%S+):(%S+),?") do -- Almost correct. getfenv()[Variable] = Value end
( ͡• ◡ ͡•) -=[ RAP: 414,347 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:30 PM |
That's not even sort of right.
iderpydogz already solved it (although maybe you wanna tonumber all the digits) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:31 PM |
I was trying to be more practical in the event that a variable doesn't start with a digit.
( ͡• ◡ ͡•) -=[ RAP: 413,739 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:32 PM |
that moment when the gsub has spoken print(("i am cool"):gsub("am","not")) |
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:36 PM |
You'd want to separate them by : not by %s. something like ",?(.-):([^,]+)"
|
|
|
| Report Abuse |
|
|
|
| 06 Oct 2016 01:38 PM |
I see. I'm fairly new to string patterns as they've always confused me.
( ͡• ◡ ͡•) -=[ RAP: 413,759 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•) |
|
|
| Report Abuse |
|
|