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 » Scripters
Home Search
 

Re: Array stuff.

Previous Thread :: Next Thread 
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:07 AM
How would I go about making a system put the array forward or backwards so many steps?

Like so:
array = {'pie','apple','idk','45'}
two forward
array = {'idk','45','pie','apple'}

Or something like that..


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:09 AM
local top = #array + 1
for idx = 1, #array/2 do
array[idx], array[top - idx] = array[top - idx], array[idx]
end
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:19 AM
Thanks, i'll try that.
I just made a failure:

array = {'pie','42','apple','nope','SDK'}

function forward(t, amt)
local new, f = {}, true
for i=1, #t do
if f then
f = not f
table.insert(new, array[amt])
elseif i+amt > #t then
for i=1, amt do
table.insert(new, array[i])
end
else
table.insert(new, array[i])
end
end
print(unpack(new))
print('Old: ',unpack(t))
end

forward(array, 2)


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:20 AM
Wait. I read what you wanted wrong.
You don't want to reverse a table you just want to flip every 2 values?
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:23 AM
Think of it as a wheel when you turn it, it shows the numbers in a circle so this is just turning the array like a wheel.


So when you reach the end you start at the start again.


Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:29 AM
Works a little now?
array = {'pie','42','apple','nope','SDK'}

function forward(t, amt)
local new, n = {}, 0
for i=1, #t do
if i+amt > #t then
n = n + 1
table.insert(new, t[n])
else
table.insert(new, t[i+amt])
end
end
print('New:', unpack(new))
print('Old:', unpack(t))
end

forward(array, 2)


Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:33 AM
--Haha, I got this working. xD

array = {'pie','42','apple','nope','SDK'}

function backwards(t, amt)
local new, n = {}, 0
for i=1, #t do
if i+amt > #t then
n = n + 1
table.insert(new, t[n])
else
table.insert(new, t[i+amt])
end
end
print('Old:', unpack(t))
print('New:', unpack(new))
end

backwards(array, 2)


Well... even if I do make both they would be two different functions how would I combine them into one as compact as possible?


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:40 AM
Ah so rotation. Do you want the same array to be flipped or do you want to be returned a new array?
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:42 AM
Doesn't matter either way, that's an easy edit.


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:54 AM
Here's 1 way of doing it, it returns a new table:

local function cycle(arr, n)
local len = #arr
local tbl = { }
for idx = 1, len do
local otherIdx = ((idx + n - 1) % len) + 1
tbl[otherIdx] = arr[idx]
end
return tbl
end

local x = { 1, 3, 5, 7 }
local y = cycle(x, -1) -- cycles left once
local z = cycle(x, 1) -- cycles right once
print(table.concat(y, " ")) -- 3 5 7 1
print(table.concat(z, " ")) -- 7 1 3 5
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 04:56 AM
Will it do it more then once or do I need to use a for i loop for that?


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:57 AM
Put any number you want. I put 1 for clarity but you can do any integer you want and it won't make it any slower or faster.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 04:59 AM
as in:
cycle(x, -15) -- cycles left fifteen times
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 05:05 AM
Are you referring to the character it'd represent? (http://www.asciitable.com/)
If so, you can do:

string.char(num) to return the character as a string itself.

string.char(65) --> "A"
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 05:06 AM
did your post get deleted O_o?
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:08 AM
No it said it's blocked three times now... *FP*

And no not the character.

It's random numbers from 0-9 but I grouped it into two so it'll cut down the length of the string by 50% if you could give me an idea how to cut it even more that'd be great all it is is a bunch of numbers 0-9 all across.


Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:10 AM
I'm just cutting it up with this thing I made:

local array = {}
for i = 1, s:len()/2 do
local new
if i == 1 then
new = s:sub(i, i+1)
else
new = s:sub((i*2)-1, i*2)
end
table.insert(array, new)
end


Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:14 AM
Still not sure how i'll cut it down still.

Because it needs to remember the old value still.

So something like 384783748238 will be turned into a bunch of random letters and symbols half the size; however, if you could make it compact it more then 50% that would be grate!


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 05:15 AM
I still don't quite understand what you want ;c
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:17 AM
Let's say I have a giant number. 87548564658268435457463527643756934180231540354382755245452643523454354235
How would I cut it in half and then revert it to the old number later or even more then half?(As small as possible.)


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 05:23 AM
What you want (assuming you have a giant string of digits, not actually a number) is essentially compression.

My idea would be to split every 2 digits into a single byte to cut it in half because that's very simple to do. There are more dramatic ways, like maybe treat all 9 digits as a 4-byte integer and store it as such, or maybe you can save more by storing every 15 digits into an 8-byte double (if we were in Lua 5.3, you'd be able to do every 19 digits into an 8-byte integer but we're not).

So
"124321" becomes "\12\34\21" (from 6 bytes to 3) in the first case.
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:26 AM
I was looking around and I found something that sounds perfect, hexatridecimal.
But, how would I use it on roblox?


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
11 Jul 2016 05:29 AM
Is your number a number or a string of digits?
Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:30 AM
Well it could have a 0 at the start so I keep it as a string so it won't remove my lead zero.


Report Abuse
KingOfInsertWars is not online. KingOfInsertWars
Joined: 30 Aug 2010
Total Posts: 162
11 Jul 2016 05:32 AM
Base 36 seems useful, but i'm not sure how to implement it on roblox.


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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