generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

How can I achieve this with a for loop rather than loadstring?

Previous Thread :: Next Thread 
Combrad is not online. Combrad
Joined: 18 Jul 2009
Total Posts: 11025
30 Oct 2011 09:46 AM
So, that would convert:

msg2 = "Ohai;How;are;you;?"
Divider = ";"
loadstring('ohr = {"'..string.gsub(msg2,Divider,'","')..'"}')()

to:

{"Ohai","How","are","you","?"}

I want to do it with a loop.
Report Abuse
NecroBumpist is not online. NecroBumpist
Joined: 12 Sep 2010
Total Posts: 4198
30 Oct 2011 10:31 AM
local function parse(input)
    local output = {}; -- the table we will store everything in
    input = input .. ";" -- add another separator, will make parsing super easy

    for str in input:gmatch("(.-);") do -- look for patterns, matching any characters between a semicolon
        output[#output + 1] = str; -- add the string to the output
    end

    return output; -- return
end

local tab = parse("Ohai;how;are;you;?");

for i = 1, #tab do
print(i, tab[i]);
end

>1 Ohai
>2 how
>3 are
>4 you
>5 ?
Report Abuse
nickcool9999 is not online. nickcool9999
Joined: 04 Dec 2007
Total Posts: 1528
30 Oct 2011 10:33 AM
msg2 = "Ohai;How;are;you;?"
Divider = ";"
function explode(str,div,ignorespaces)
if div=='' then return end
local tab = {}
local cur = ""
for i = 1, string.len(str) do
stat1 = stat1 + 1 -- See what i mean?
if str:sub(i,i) ~= enter and (ignorespaces == true or str:sub(i,i) ~= " ") then
if str:sub(i,i) == div then
tab[#tab+1] = cur
cur = ""
else
cur = cur .. str:sub(i,i)
end
end
end
if cur ~= "" then
tab[#tab+1] = cur
cur = ""
end
return tab
end

local t = explode(msg2, Divider, true)

-- efficiency. Loadstring is BAD BAD BAD. do not use it to get a variable or you will regret it later.
Report Abuse
nickcool9999 is not online. nickcool9999
Joined: 04 Dec 2007
Total Posts: 1528
30 Oct 2011 10:34 AM
And if you want it to ignore enters, put this at the start of the script.

local enter = ([[l
l]]):sub(2, 2)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image