Scripting > mrScriptBasic overview > mrScriptBasic examples > 1: Using objects and iterating through collections
 
1: Using objects and iterating through collections
In mrScriptBasic you can access the functions of any objects that are available on your system. This example gives a simple illustration of using a standard Microsoft object to create a text file and an UNICOM Intelligence Data Model object to get a list of the functions in the UNICOM Intelligence Function Library.
The code iterates through the Evaluate component's Functions collection object and writes the name and parameters of each function to a text file.
Code
This example is included with the UNICOM Intelligence Developer Documentation Library as a sample script called Functions.mrs. See Sample mrScriptBasic files for more information.
Dim Func, Param, expProgram, strFunctions, fso, txtfile ' Line 1

Set fso = CreateObject("Scripting.FileSystemObject") ' Line 3
Set txtfile = fso.CreateTextFile("C:\Program Files\IBM\SPSS\DataCollection\7\DDL\Output\Functions.txt", True)

Set expProgram = CreateObject("mrEvaluate.Evaluate") ' Line 6
For Each Func in expProgram.Functions
strFunctions = strFunctions + Func.Name + "("
For Each Param in Func.Parameters
strFunctions = strFunctions + Param + " "
Next
strFunctions = strFunctions + ") "
txtfile.WriteLine (strFunctions) ' Line 13
strFunctions = ""
Next

txtfile.Close() ' Line 17
Line 1
Declares the variables used in the code. You always need to declare the variables you use in the code unless you put an Option Implicit statement on the first line.
Line 3
Uses the CreateObject function to create a FileSystemObject, which is a standard Microsoft object for working with folders and files. The FileSystemObject.CreateTextFile method is then called to create a text file object. A Set statement is used so that the text file object can be used in the script.
Line 6
Uses the CreateObject function again. This time to create an Evaluate object, which is the main evaluation and execution engine of the UNICOM Intelligence Data Model's Evaluate component. For information on the Evaluate component objects, see the Evaluate Component Object Model.
Line 7
Uses the For Each...Next statement to iterate through the functions in the UNICOM Intelligence Function Library, setting up for each function a text string consisting of the function's name and parameters. Notice that the + operator is used to concatenate the strings. (In mrScriptBasic you cannot use an ampersand (&) to concatenate strings as you can in VBScript.) To get details of the parameters, the example uses another For Each...Next statement to iterate through the function's Parameters collection.
Line 13
Calls the WriteLine method of the text file object to write the text string to the text file.
Line 17
Calls the Close method of the text file object to close the text file. Notice that in mrScriptBasic you need to include the brackets even though the method does not return a value.
Result
Here is the text file viewed in NotePad:
See also
2: Getting a list of valid languages
mrScriptBasic examples