Create multiple global variables from an array

I had a situation where I needed to separate out each element of an array (in my case a Multi-dimensional array) to their own globals so I created a fn to do so.

It can set the globals to undefined also.

I made it work with any array type while I was at it.

It's a very edge case fn but here's the code for anyone that may need it.

If you have another method that doesn't involve the use of the execute function then I'd be happy to hear that. Or if you have any comments then please let me know.

(
 
  cnt = undefined ; local cnt = 13
 
  -- String class example.
  --   myArr = undefined ; local myArr = for i = 1 to cnt collect (i as string + 12 as string)
 
  -- Checker class example.
  --   myArr = for i = 1 to cnt collect checker()
 
  -- Integer class example.
  --   myArr = undefined ; local myArr = for i = 1 to cnt collect i
 
  -- Multi-dimensional Array
  myArr = undefined ; local myArr = #(#(1,2,3,4,5,6,7,8,9,10),#(10,20,30,40,50,60,70,80,90,100))
 
  -- varBatchCreateGlobals creates separate globals from the given array or make undefined
  -- varName: The Name of the global variable as a string.
  -- arr: Can be numbers, strings, arrays and multi-dimensional arrays.
  -- varCreate: true to create or false to set the global variables to undefined.
  -- formatOn: true to print the results (Useful for testing) or false not print them out.
  fn varBatchCreateGlobals varName arr varCreate:false formatOn:false =
  (
    if varCreate then (
      if formatOn do format "Creating % % variables...\n" arr.count "global"
      -- Create the variables.
      res = for i = 1 to arr.count do (
        global TDWB_varBatchCreateGlobalArr = arr -- Set the arr to a global so that it can be accessed in the execute fn below.
        execute ("global " + varName + i as string + " = " + "TDWB_varBatchCreateGlobalArr[" + i as string + "]")
        TDWB_varBatchCreateGlobalArr = undefined -- Set the TDWB_varBatchCreateGlobalArr to undefined.
        -- Prints out the variables if they're defined.
        if formatOn do (
          if (execute(varName + i as string + " != undefined")) do (
            format "%\tvariable\t= %\n" (varName + i as string) (execute(varName + i as string)))
          format "%\tclassof\t\t= %\n" (varName + i as string) (classof(execute(varName + i as string)))
          )
        )
      if formatOn do format "Created % % variables...\n" arr.count "global"
      ) else (
      -- Sets all the variables to undefined.
      for i = 1 to arr.count do (execute ("global" + " " + varName + i as string + " = undefined"))
      if formatOn do format "Deleted % % variables...\n" arr.count "global"
      )
      ) -- End varBatchCreateGlobals
 
  varBatchCreateGlobals "TDWB_GB_Arr_" myArr varCreate:true formatOn:true
 
  )
 
ok
 
/*
myArr = undefined ; local myArr = #(#(1,2,3,4,5,6,7,8,9,10),#(10,20,30,40,50,60,70,80,90,100))
This example returns:
Creating 2 global variables...
TDWB_GB_Arr_1 variable  = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
TDWB_GB_Arr_1 classof   = Array
TDWB_GB_Arr_2 variable  = #(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
TDWB_GB_Arr_2 classof   = Array
Created 2 global variables...
*/

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jahman's picture

.

sure it is possible
if you want to keep assigned arrays separated from the main array you use deepCopy

(
 
  cnt = undefined ; local cnt = 13
 
  -- String class example.
  --   myArr = undefined ; local myArr = for i = 1 to cnt collect (i as string + 12 as string)
 
  -- Checker class example.
  --   myArr = for i = 1 to cnt collect checker()
 
  -- Integer class example.
  --   myArr = undefined ; local myArr = for i = 1 to cnt collect i
 
  -- Multi-dimensional Array
  myArr = undefined ;
	local myArr = #(
	  #(1,2,3,4,5,6,7,8,9,10),
	  #(10,20,30,40,50,60,70,80,90,100)
  )
 
  -- varBatchCreateGlobals creates separate globals from the given array or make undefined
  -- varName: The Name of the global variable as a string.
  -- arr: Can be numbers, strings, arrays and multi-dimensional arrays.
  -- varCreate: true to create or false to set the global variables to undefined.
  -- formatOn: true to print the results (Useful for testing) or false not print them out.
	fn varBatchCreateGlobals varName arr varCreate:false formatOn:false =
	(
		if varCreate then
		(
			if formatOn do format "Creating % % variables...\n" arr.count "global"
 
			for i = 1 to arr.count do
			(
				var_name = varName + i as string
				execute var_name
 
				globalVars.set var_name (deepCopy arr[i])
 
				if formatOn do
				(
					if (var = globalVars.get var_name) != undefined do
					(
						format "%\tvariable\t= %\n" var_name var
					)
					format "%\tclassof\t\t= %\n" var_name (classof var)
				)
			)
 
			if formatOn do format "Created % % variables...\n" arr.count "global"
		)
		else
		(
		  for i = 1 to arr.count do globalVars.remove (varName + i as string)
 
		  if formatOn do format "Deleted % % variables...\n" arr.count "global"
		)
	)
 
	varBatchCreateGlobals "Test_" myArr varCreate:true formatOn:true
	varBatchCreateGlobals "Test_" myArr varCreate:false formatOn:true
 
)
 
ok
3dwannab's picture

Thanks

I think I got too carried away with the execute fn!!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.