*
Metamerge logo
Search

Advanced Search
*
*
*
* HOME DOCUMENTS & RESOURCES DOWNLOADS EARLY TECH ACCESS SUPPORT FAQ KNOWN ISSUES OLD VERSIONS
*

Using floating point values in scripting

The following examples demonstrate how floating point values can be used within the scripting code you create. All of the following examples are implemented in JavaScript. While the same examples may be repeated using several other scripting languages, the syntax may be different:

o The simple script below assigns floating point values to two variables in order to find their average. This code may be executed from any scripting control point. The log file output will always read "r = 3.85".

    var a = 5.5;
    var b = 2.2;
    var r = (a + b) / 2;
    task.logmsg("r = " + r);

o The next example extends the simple script presented above. Consider you have in your input Connector a multiple values attribute called "Marks" containing string values (java.lang.String) representing floating point values (a common situation in real life). This attribute is mapped to an attribute in your output Connector called "AverageMark", which holds the average value of the "Marks" attribute's values. Below is the code used to correctly map these attributes:

01.    var values = work.getAttribute("Marks").getValues();
02.    var sum = 0;
04.    var count = 0;
05.    for (i=0; i<values.length; i++)
06.    {
07.        sum = sum + new Number(java.lang.Double(values[i]));
08.        count++;
09.    }
10.    var average = (count > 0) ? (sum / count) : 0;

11.    avr_attr = system.newAttribute("AverageMark");
12.    avr_attr.addValue(average);
13.    ret.value = avr_attr;

In this example, the core of the floating point processing takes place at line 7 . It first parses the attributes's string value [ java.lang.Double(values[i]) ] and then creates a numeric value to use in scripting [ new Number(java.lang.Double(values[i])) ]. Line 10 calculates the average value and assigns it to a variable. Lines 11-13 assign the calculated value as the output attribute value, thereby completing the mapping operation.

 

*
  Metamerge Integrator version 4.5 ©Copyright Metamerge AS 2000-2002 Last edited 2002-04-30 contact us