Blue Eye Macro
http://blueeye-macro.com/

How to update values in a collection
http://blueeye-macro.com/viewtopic.php?f=123&t=34970
Page 1 of 1

Author:  stakemaster [ Mon Nov 04, 2019 5:51 pm ]
Post subject:  How to update values in a collection

The following contains a method for updating a numeric collection being used to count the occurrences of generated random numbers between 1 and 10. Adapting the code for other purposes is fairly simple. The core method makes use of a second collection and maintains the original - see function("Update Counter Collection"). The macro updates the collection 1000 times and displays the resulting collection on completion. The code is a complete and runnable macro:
Code:
 
 begin
     // example of how to update a collection of counters with the count of the individual
     // numbers selected by a random number statement
     Function.Execute("Setup Collection")
     Function.Execute("Main Process")
 end

function("Main Process")
     begin
          Variable.Set("Iterations", "1000")
          Begin loop("{Iterations}")
               // get a random number between 1 and 10 to indicate the counter to be updated
               Variable.Set random number("RN", "1", "10")
               // get the current value of the relevant counter & increment it
               Variable.Get from collection("Counter", "{RN}", "CurrentValue")
               Variable.Increment (Math)("CurrentValue")
               Variable.Copy("CurrentValue", "NewValue")
               // update the counter collection at item RN with the incremented current value (NewValue)
               Function.Execute setting 2 variables("Update Counter Collection", "Index", "{RN}", "NewValue", "{NewValue}")
          end
          // Display Counter Collection
          Variable.Set("strText", "Counter Collection Values")
          Variable.Evaluate (Text)("Iterations = {Iterations}", "displayInfo")
          Variable.Append line("strText", "{displayInfo}")
          Variable.Append line("strText", " ")
          Variable.Set("Sum", "0")
          begin loop("{CollectionCount}")
               Macro.Get parent loop iteration("LoopCount")
               Variable.Get from collection("Counter", "{LoopCount}", "CurrentValue")
               Variable.Evaluate (Text)("{LoopCount}. {CurrentValue}", "displayInfo")
               Variable.Append line("strText", "{displayInfo}")
               Variable.Add (Math)("Sum", "{CurrentValue}")
          end
          Variable.Append line("strText", " ")
          Variable.Evaluate (Text)("Total = {Sum}", "displayInfo")
          Variable.Append line("strText", "{displayInfo}")
          Window.Display message box("{strText}", "Yes")
     end
function

function("Update Counter Collection")
     begin
          Variable.Clear collection("Work")
          Variable.Count collection("Counter", "CollectionCount")
          // loop through collection
          begin loop("{CollectionCount}")
               Macro.Get parent loop iteration("LoopCount")
               // update Work collection with new value if iteration = passed index
               if  Variable.Is equal to("LoopCount", "{Index}")
                    begin
                         Variable.Add to collection("Work", "{NewValue}")
                    end
               // just add Counter entry to Work collection if iteration <> passed index
               if  Variable.Is not equal to("LoopCount", "{Index}")
                    begin
                         Variable.Get from collection("Counter", "{LoopCount}", "CurrentValue")
                         Variable.Add to collection("Work", "{CurrentValue}")
                    end
          end
          // copy Work collection back to Counter collection
          Variable.Clear collection("Counter")
          begin loop("{CollectionCount}")
               Macro.Get parent loop iteration("LoopCount")
               Variable.Get from collection("Work", "{LoopCount}", "CurrentValue")
               Variable.Add to collection("Counter", "{CurrentValue}")
          end
     end
function

function("Setup Collection")
     begin
          // Count Collection
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
          Variable.Add to collection("Counter", "0")
     end
function

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/