// CalculateMean
//
// This is an example of a macro that is designed to be
// called from other macros using the runMacro() function. 
// It uses the split() function to extract the values from
// the string passed to it by runMacro(), calculates the mean,
// then returns it to the calling macro as a string. To test it,
// save this file in the macros folder, select the last three
// lines, then use use the File>Run Macro command (ctrl-r).

values = getArgument();
return toString(mean(values));
exit;

function mean(values) {
    if (lengthOf(values)==0)
        return 0;
    a = split(values, "");
    sum = 0;
    for (i=0; i<a.length; i++) {
        n = parseFloat(a[i]);
        if (isNaN(n))
            exit("'" + a[i] + "' is not a number");
        sum += n;
    }
    return sum/n;
}

// This example shows how to call this function from another macro.
// It assumes the "CalculateMean.txt" is in the macros folder.
mean = runMacro("CalculateMean", "1.5 -2.5 3.0");
mean = parseFloat(mean); // convert to number
print("mean="+mean);
