Showing posts with label scatter. Show all posts
Showing posts with label scatter. Show all posts

Monday, June 18, 2012

[JScript] Scatter redux

The previous version has a problem: when the recordset has zero records, it falls over. The updated version is below:

function Scatter( o ) {
 var result = {};
 var bEmpty = true;
 var vba = {};
 if ( o.RecordCount > 0 ) {
  bEmpty = false;
  vba = new VBArray( o.GetRows() );
 }
 var fldCount = o.Fields.Count;
 for (var i = 0; i < fldCount; i++ ) {
  result[o.Fields(i).Name] = [];
  if ( ! bEmpty ) {
   for ( var j = vba.lbound(2); j <= vba.ubound(2); j++ ) {
    result[o.Fields(i).Name].push( vba.getItem(i,j) );
   }
  }
 }
 return result;
}


© Copyright Bruce M. Axtens, 2012

Saturday, June 16, 2012

[JScript] Scatter (like the FoxPro function)

On a roll tonight!

Scatter and Gather are a pair of functions in FoxPro and related languages which would take a record and copy it into the memvar space, naming the memvar the same as the field name and storing the field's value. Gather did the opposite.

Now I've got that working in JScript. I hand it a recordset and it creates an object with the field names as property names and the values of each as a list attached to the property. This makes things like this possible:

var X = Scatter( oRS );
 for (var i = 0; i < X.AdvertiserParentFK.length; i++ ) {
  WScript.Echo( i, X.AdvertiserParentFK[i]);
 }
And so here's the function:
function Scatter( o ) {
  var result = {};
  var vba = new VBArray( o.GetRows() );
  var fldCount = o.Fields.Count;
  for (var i = 0; i < fldCount; i++ ) {
   result[o.Fields(i).Name] = [];
   for ( var j = vba.lbound(2); j <= vba.ubound(2); j++ ) {
    result[o.Fields(i).Name].push( vba.getItem(i,j) );
   }
  }
  return result;
 }

More to enjoy! 


© Copyright Bruce M. Axtens, 2012.