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
#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:
-- 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


Messages In This Thread
Simple scripting help - by Jayce - 15.12.2021, 16:22
RE: Simple scripting help - by Erwin van der Zwart - 15.12.2021, 21:24
RE: Simple scripting help - by admin - 16.12.2021, 06:31
RE: Simple scripting help - by Jayce - 16.12.2021, 11:50
RE: Simple scripting help - by admin - 16.12.2021, 12:35

Forum Jump: