Here’s an interesting scenario from a client:
We have set up our nets for TB and BB on a rating scale from 1/10 as 8/10 and 1/3, ie as T3B, B3B. But we like to experiment, and want to be able to change the (possibly hundreds) of variables to net 9/10 and 1/2 instead, ie to T2B B2B, and we might want T4B B4B, and then decide to revert. How do we set up the specs for dynamic changes, without clumsy search/replace or direct editing of *.met files?
The API for adding expressions or anything else to a codeframe is
AddToCodeFrame(targetvar, expression=label\nexpression=label\n…)
This checks to see if the expression is already present, and if so does nothing, otherwise the expression is added.
From a pure scripting perspective, the traditional solution would be to define a global string variable and use that in AddToCodeFrame, eg
'' Top3Box topbox = "8/10" AddToCodeFrame(targetvar, "_net(" & topbox & ")=TopBox")
And then at some later point I change this to T2B instead, as
'' Top2Box topbox = "9/10" AddToCodeFrame(targetvar, "_net(" & topbox & ")=TopBox")
The problem with this is: AddToCodeFrame sees a new net, and appends it, leaving the old one (the T3B) in place. You may want this, but then using just _ (underscore) in GenTab syntax to pick up all defined nets will collect both T3B and T2B, and both are labelled just “TopBox” so you cannot tell the difference.
The solution is to use Ruby’s named code lists. For discussion on how extremely useful code lists can be on FMCG jobs with sometimes thousands of brands, see page 13 of this document.
This VBScript shows how to apply code lists to the netting scenario. For top 2 and bottom 2:
#include $RubyUtilitiesLibrary.vbs #include $RubyVariablesLibrary.vbs #include $RubyReportsLibrary.vbs Sub Main() Initialise() ClearRAM() '' define the code lists for ten point scales SetCodeList "TopBox10", "9/10" SetCodeList "BotBox10", "1/2" '' make a sandpit variable from FAVRAT_1 DefGen "testvar", "FAVRAT_1($a)", "testing dynamic nets" AddLevel "a", "testvar", "FAVRAT_1", "*" GenClose Construct "testvar" '' add TB and BB AddToCodeFrame "testvar", "_net($TopBox10)=TB\n_net($BotBox10)=BB" '' check with a table GenTab "CodeListTest", "Gender", "testvar", "", "" FrequenciesOnly() RefreshRep() Cleanup() End Sub
In the AddToCodeFrame line, we use $TopBox10 and $BotBox10. The leading $ tells Ruby that the text which follows is a code list name, and the actual codes are substituted at run time. AddToCodeFrame sees that you already have “_net($TopBox10)”, so repeated script runs do nothing.
The table is:
873 = 678+195, etc.
Now, to broaden the ranges to top 3 and bottom 3, you need only edit the code list definitions in one place (from 9/10 to 8/10, and 1/2 to 1/3), regardless of how many variables have the TB and BB nets, as
'' define the code lists SetCodeList "TopBox10", "8/10" SetCodeList "BotBox10", "1/3"
The table then becomes:
1783=910+678+195, etc.
Code lists can be managed from the GUI
As always, if defined in a script, the script should be the point of master control, but the GUI can be useful for cleaning up accidental messes, and to check that the script has delivered the definitions as expected.
Code lists can be used anywhere codes appear, eg
This uses the $TopBox10 code list as a filter against a different variable, NPS_1.
Comments are closed