This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Simple scripting help
#1
Hello,

I'm having some issues finding a solution to my problem, i'm sure it is very easy but I'm not very good at scripting.
Let's say I have 5 ASCII objects with some value like value1, value2 etc.. I would like to take these values and use math.random function to "scramble" them randomly and write the randomly scrambled values into another set of 5 ASCII objects. Is something like that possible? Also the values cannot repeat, same value cannot be assigned to two of the new scrambled ASCII objects.

EDIT: Also separate question, is there a way to let's say I have ASCII group objects with values, in script i'd like to grp.getvalue them to val1, val2, val3 etc.. and after that use some cycle function to do the random scramble operation above? How would I make it so the cycle knows to move val1 to val2? Let me show example script which might make it more clear but definitely will NOT work.
val1 = "val1"
val2 = "val2"
val3 = "val3"

for (i=1, i<= 3, i++) do
val[i] = grp.write('1/1/[i]')
end

Basically this is exactly what I need to do with the first question above about scrambling them. I'd like to take val1,val2,val3 and randomize those values into 3 group addresses 1/1/1, 1/1/2, 1/1/3 without val1,val2 or val3 repeating in any of those 3 group addresses.
Sorry I know it makes no sense scripting wise but that's what I need to do.

Thank you in advanced!
Reply
#2
Well it's by far not a simple script that is why i took the challenge as i like that (:

Create your ASCII objects and add the tag "Scramble" to it and run this script to scramble all the object values without any duplicates 
Code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
-- get objects to scramble by tag scrambled = grp.tag('Scramble') -- set randomseed math.randomseed(os.time()) -- function to scramble strings function scramble(str)   local chars = {}   for char in str:gmatch'.[\128-\191]*' do     table.insert(chars, {char = char, rnd = math.random()})   end   table.sort(chars, function(a, b) return a.rnd < b.rnd end)   for i, v in ipairs(chars) do     chars[i] = v.char   end   return table.concat(chars) end -- create new values table with unique values newvalues = {} for i, object in ipairs(scrambled) do   -- Set max retries for unique values to avoid script lockout   max_retry = 100   -- Goto marker when value is already in the table or the same as old value   ::retry::   if #newvalues < 1 then     value = scramble(object.value)     if value == object.value then       max_retry = max_retry - 1       if max_retry < 1 then         return       else       goto retry       end     end   table.insert(newvalues, {address = object.address, value = value})   else     value = scramble(object.value)     if value == object.value then       max_retry = max_retry - 1       if max_retry < 1 then         return       else         goto retry       end     end     for j, newvalue in ipairs(newvalues) do       if value == newvalue.value then         max_retry = max_retry - 1         if max_retry < 1 then           return         else           goto retry         end       end     end     table.insert(newvalues, {address = object.address, value = value})   end end --Check if tables are same lenght if #newvalues == #scrambled then -- update objects for _, object in ipairs(newvalues) do    grp.checkupdate(object.address, object.value) end end
Still curious to your use case (:

BR,

Erwin
Reply
#3
@Erwin, I think you over-complicated the task there Smile

Use this if you simply want to scramble already set values for tagged objects:
Code:
1234567891011121314
math.randomseed(os.time()) objects = grp.tag('Scramble') values = {} for _, obj in ipairs(objects) do   values[ #values + 1 ] = obj.value end for _, obj in ipairs(objects) do   index = math.random(1, #values)   obj:write(values[ index ])   table.remove(values, index) end
Reply
#4
Thank you both guys! Works flawlessly, I've also learned some stuff too.
Reply
#5
(16.12.2021, 06:31)admin Wrote: @Erwin, I think you over-complicated the task there Smile

Well i don't fully agree, your version is just rotating the values between the objects, my version is realy scrambling the values and checks there are no duplicted values at any time..
Reply
#6
@Erwin, sure thing Smile The problem here is that the task is not defined clearly.
Reply
#7
I think both where asked  Smile
Reply


Forum Jump: