add time from regex fields in script

Help with writing and running scripts

add time from regex fields in script

Postby sklad2 » Fri Aug 08, 2008 9:27 pm

I am having a problem wit this data
Code: Select all
kjg oejhgreg

eig
qegj rpqej gpq
egj 'pierp'
erg
   r
   gu eg
   u g
   p  u g
   p  u g
   p  u g
   p  ue gp
   eg pue  gpue qg
Elapsed: 01:10:00.66
'lgk sdl;jgad;ojg ad
gag
fagk
fa
gka
gk
Elapsed: 00:33:44.00
agad
gfa
g
ga
g
ag
ag
g
Elapsed: 00:00:55.23
ag
ag
adgf
ag
ad
gfa
dh
dhs
fhf
jhdsjy
dgj
gj

Elapsed: 00:08:32.77
h
fgd
gfswd
hgwr
hwr
ht
wrh
wrh
wrh
w


Elapsed: 00:50:00.12
ashaswhtwsrhwrthrwhwrthwrthwrth
sfdth
wrh
wr
hw45
h45w
h
w456h
2456h
52w6
h526
h562
h5
h5
64h5
h5
h56
h
Elapsed: 00:16:00.01
wiy rtpio3qytgpiu31qy gtipuq3yh tg3
Elapsed: 00:19:00.40
e
rtweb5
tw
5tbqw43
b6tq3
6q6
q6
23
324
6
Elapsed: 00:31:00.56
qt
34b63
6
6
q26
w234
562
4652
4b6
2
Elapsed: 03:00:00.12
rtqw
4etyb23q4
6234
62q43
62q34
562q
462q43
62q
66

Elapsed: 00:00:44.11
twe
y245b
67y24
7y642w
y7w4
y75n w4
57y6nw 4
5b
56 46
7w7
45
7nw475

Elapsed: 00:00:22.99
h
e5thn
5 h
2 54y6
2y
45 y
y4
y
4y5
y45
45y
y4
2y
654y

Elapsed: 00:00:10.03
ka
;lkgv
kbf
fbgk
qewbg
wqre
bgt
qwertbgk
q4rekgt
q4tkg
4q1gk
4qpg
1435jgt
1j435gh
Elapsed: 00:00:04.00
iah epfghqepuoir gpueiqgypuiqeygipuqeyguiqg:
Elapsed: 00:00:00.00


here is the code to add the times up and print in the output window, but it is not working all help is appreciated

Code: Select all
// Define working environment for this file.
UltraEdit.perlReOn();
UltraEdit.insertMode();
UltraEdit.columnModeOff();
var t1,t2,t3,ttime
 
// Helper function as UE does not recognize start and ending slashes in regexp
RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending slashes */
 
// Where to find the parmfile:
var parmfile = "c:\\temp\\sample_timing.txt";
 
// Now go get parm file and parse it into an array of parameters
var parms = getParms(parmfile);

 UltraEdit.outputWindow.write( parms.length );
if(parms.length != 0) {
 
 
for(var i=0;i<parms.length;i++) {
 
   
      ttime= ttime + parms[i];
 
    }
  }
 
 t1 = parseInt( ttime/3600 );
 
 
 



 


/* Parse parameter document to get parameters */
function getParms(parmfilePath) {
  var parsedParms = new Array();
 
  // Open parm file if not open already
  var parmdocIx = getDocumentIndex(parmfilePath);
  var parmdocOpen = true;
  if(parmdocIx==-1) { /* not open */
    UltraEdit.open(parmfilePath);
    parmdocIx = getDocumentIndex(parmfilePath);
    parmdocOpen = false;
  }
 
  if(parmdocIx==-1) { /* just in case, no parm file found */
    return parsedParms;
  }
 
  // Define all the replace options to correct execute the follwing 2 replace all commands.
  UltraEdit.document[parmdocIx].findReplace.mode=0;
  UltraEdit.document[parmdocIx].findReplace.searchDown=true;
  UltraEdit.document[parmdocIx].findReplace.searchInColumn=false;
  UltraEdit.document[parmdocIx].findReplace.matchCase=false;
  UltraEdit.document[parmdocIx].findReplace.matchWord=false;
  UltraEdit.document[parmdocIx].findReplace.preserveCase=false;
  UltraEdit.document[parmdocIx].findReplace.regExp=true;
 
  var reParms = /Elapsed: ([\d]{2}:){2}[\d]{2}\.[\d]{2}/; /* perl regular expression */
 
  UltraEdit.document[parmdocIx].top();
  // Loop and find all parameters
  while (UltraEdit.document[parmdocIx].findReplace.find(reParms.toUEregexp())) {
    // retrieve found parameter definition
    var foundParmDefs = UltraEdit.document[parmdocIx].selection;
 
// UltraEdit.outputWindow.write( foundParmDefs )
 t1 = foundParmDefs.substr(9,2);
 t2 = t1 * 3600;
 t1 = foundParmDefs.substr(12,2);
 t2 = t2 + ( t1 * 60 );
 t1 = foundParmDefs.substr(15,5);
 t2 = t2 + t1;
 // add up total in seconds format is sssssss.hh
 // after adding I only care about sssssss
// t3 = t3 + t2;



    // collect pairs of parameter definitions into array:
   parsedParms.push( [parseInt(t2)] );
  }
 
  if(parmdocOpen==false) { /* not open, then close again on exit */
    UltraEdit.closeFile(UltraEdit.document[parmdocIx].path,2);
  }
 
  return parsedParms;
}
 
 

 
/* getDocumentIndex(filepath) */
/* If called without parameter = Find the tab index of the active document */
/* If called with parameter "filepath" = Find the tab index of that document. -1 = document not found */
function getDocumentIndex(filepath) {
  var tabindex = -1; /* start value */
 
  for (i = 0; i < UltraEdit.document.length; i++)
  {
    if(filepath) {
      if (UltraEdit.document[i].path.toLowerCase().indexOf(filepath.toLowerCase())!=-1) {
        tabindex = i;
        break;
      }
    }
    else {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
        tabindex = i;
        break;
      }
    }
  }
  return tabindex;
}
 
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am

Re: add time from regex fields in script

Postby sklad2 » Sat Aug 09, 2008 3:29 pm

Any help in making this script better would be appreciated.

Code: Select all
// Define working environment for this file.
UltraEdit.perlReOn();
UltraEdit.insertMode();
UltraEdit.columnModeOff();
var t1,t2,t3,t4,ttime
 
// Helper function as UE does not recognize start and ending slashes in regexp
RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending slashes */
 
// Where to find the parmfile:
var parmfile = "c:\\temp\\sample_timing.txt";
 
// Now go get parm file and parse it into an array of parameters
var parms = [] ;
getParms(parmfile);

 UltraEdit.outputWindow.write( ' ' + parms.length );
if(parms.length != 0) {
 
 ttime = 0;
 
for(var i=0;i<parms.length;i++) {
       ttime += parms[i];
     
       t1 = parseInt((parms[i]/100)/3600);
       t2 = parseInt(( (parms[i]/100) -( t1 * 3600) )/60);
       t3 = parseInt((parms[i]/100) - ( t1 * 3600) - ( t2 * 60));
       
       

   UltraEdit.outputWindow.write( "Step" + ( i + 1 ) + " ran for " + parseInt(t1) + " hrs " + parseInt(t2) + " min " + parseInt(t3) + " sec"  );
     
     
 
    }
  }
 
       t1 = parseInt((ttime/100)/3600);
       t2 = parseInt(( (ttime/100) -( t1 * 3600) )/60);
       t3 = parseInt((ttime/100) - ( t1 * 3600) - ( t2 * 60));
       
       

   UltraEdit.outputWindow.write( "Total run time " + parseInt(t1) + " hrs " + parseInt(t2) + " min " + parseInt(t3) + " sec"  );

 
 



 


/* Parse parameter document to get parameters */
function getParms(parmfilePath) {
  var parsedParms = new Array();
 
  // Open parm file if not open already
  var parmdocIx = getDocumentIndex(parmfilePath);
  var parmdocOpen = true;
  if(parmdocIx==-1) { /* not open */
    UltraEdit.open(parmfilePath);
    parmdocIx = getDocumentIndex(parmfilePath);
    parmdocOpen = false;
  }
 
  if(parmdocIx==-1) { /* just in case, no parm file found */
    return parsedParms;
  }
 
  // Define all the replace options to correct execute the follwing 2 replace all commands.
  UltraEdit.document[parmdocIx].findReplace.mode=0;
  UltraEdit.document[parmdocIx].findReplace.searchDown=true;
  UltraEdit.document[parmdocIx].findReplace.searchInColumn=false;
  UltraEdit.document[parmdocIx].findReplace.matchCase=false;
  UltraEdit.document[parmdocIx].findReplace.matchWord=false;
  UltraEdit.document[parmdocIx].findReplace.preserveCase=false;
  UltraEdit.document[parmdocIx].findReplace.regExp=true;
 
  var reParms = /Elapsed: ([\d]{2}:){2}[\d]{2}\.[\d]{2}/; /* perl regular expression */
 
  UltraEdit.document[parmdocIx].top();
  // Loop and find all parameters
  while (UltraEdit.document[parmdocIx].findReplace.find(reParms.toUEregexp())) {
    // retrieve found parameter definition
    var foundParmDefs = UltraEdit.document[parmdocIx].selection;
 
// UltraEdit.outputWindow.write( foundParmDefs )
 t1 = foundParmDefs.substr(9,2);
 t2 = foundParmDefs.substr(12,2);
 t3 = foundParmDefs.substr(15,5);

 t4 = parseInt( t1 * 360000 ) + parseInt( t2 * 6000 ) + parseInt(t3 * 100);
 UltraEdit.outputWindow.write( t1 + " " + t2 + " " + t3 + " " + t4);

 // add up total in seconds format is sssssss.hh
 // after adding I only care about sssssss




    // collect pairs of parameter definitions into array:
   parms.push(  t4 );
   
 

  }
 

 
 
}
 
 

 
/* getDocumentIndex(filepath) */
/* If called without parameter = Find the tab index of the active document */
/* If called with parameter "filepath" = Find the tab index of that document. -1 = document not found */
function getDocumentIndex(filepath) {
  var tabindex = -1; /* start value */
 
  for (i = 0; i < UltraEdit.document.length; i++)
  {
    if(filepath) {
      if (UltraEdit.document[i].path.toLowerCase().indexOf(filepath.toLowerCase())!=-1) {
        tabindex = i;
        break;
      }
    }
    else {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
        tabindex = i;
        break;
      }
    }
  }
  return tabindex;
}
 
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am

Re: add time from regex fields in script

Postby jorrasdk » Sun Aug 10, 2008 7:38 am

sklad2 wrote:Any help in making this script better would be appreciated.


Ok, too much time on my hand (or maybe just an excuse not to do the dishes ;-) )

I have introduced the Javascript Date() object into your script:
http://www.w3schools.com/jsref/jsref_obj_date.asp

otherwise I have renamed some variables so they relate to what they contain. I also added one of my javascript favorites: The destructuring assignment in combination with Regexp exec method:

var [ ,hrs,mins,secs,milli ] = reElapseTime.exec(foundElapseTime);

I hope it was Ok to go beyond fixing whatever errors were in the script ! Have fun!

Code: Select all
// Define working environment for this file.
UltraEdit.perlReOn();
UltraEdit.insertMode();
UltraEdit.columnModeOff();

// Helper function as UE does not recognize start and ending slashes in regexp
RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending slashes */

// Helper function for Date object to return total hours (can exceed 24 hours)
Date.prototype.getTotalHours = function() { return parseInt(this.getTime()/3600000,10); }

// Where to find the timing file:
var timingfile = "c:\\temp\\sample_timing.txt";

// Now go get timing file and parse it into an array of elapse times
var elapseTimes = getElapsetimes(timingfile);

var ttime = 0;
UltraEdit.outputWindow.write( ' ' + elapseTimes.length );
if(elapseTimes.length !== 0) {

   for(var i=0;i<elapseTimes.length;i++) {
      ttime += elapseTimes[i].getTime(); /* get millis since 1/1-1970 and add to total */

      UltraEdit.outputWindow.write( "Step" + ( i + 1 ) + " ran for " + elapseTimes[i].getUTCHours() + " hrs " + elapseTimes[i].getUTCMinutes() + " min " + elapseTimes[i].getUTCSeconds() + " sec"  );
   }
}

var totalElapse = new Date();
totalElapse.setTime(ttime); /* set time for totalElapse using summed up milli secs */

UltraEdit.outputWindow.write( "Total run time " + totalElapse.getTotalHours() + " hrs " + totalElapse.getUTCMinutes() + " min " + totalElapse.getUTCSeconds() + " sec"  );

/* Parse parameter document to get parameters */
function getElapsetimes(timingFilepath) {
   var parsedElapseTimes = new Array();
   
   // Open parm file if not open already
   var timingDocix = getDocumentIndex(timingFilepath);
   var timingDocopen = true;
   if(timingDocix==-1) { /* not open */
      UltraEdit.open(timingFilepath);
      timingDocix = getDocumentIndex(timingFilepath);
      timingDocopen = false;
   }

   if(timingDocix==-1) { /* just in case, no parm file found */
      return parsedElapseTimes; /* empty array */
   }

   // Define all the replace options to correct execute the follwing 2 replace all commands.
   UltraEdit.document[timingDocix].findReplace.mode=0;
   UltraEdit.document[timingDocix].findReplace.searchDown=true;
   UltraEdit.document[timingDocix].findReplace.searchInColumn=false;
   UltraEdit.document[timingDocix].findReplace.matchCase=false;
   UltraEdit.document[timingDocix].findReplace.matchWord=false;
   UltraEdit.document[timingDocix].findReplace.preserveCase=false;
   UltraEdit.document[timingDocix].findReplace.regExp=true;

   var reElapseTime = /Elapsed: ([\d]{2}):([\d]{2}):([\d]{2})\.([\d]{2})/; /* perl regular expression */

   UltraEdit.document[timingDocix].top();
   // Loop and find all parameters
   while (UltraEdit.document[timingDocix].findReplace.find(reElapseTime.toUEregexp())) {
      // retrieve found parameter definition
      var foundElapseTime = UltraEdit.document[timingDocix].selection;
     
      // assign using destructuring assignment and "reparsing" with regexp exec method:
      var [ ,hrs,mins,secs,milli ] = reElapseTime.exec(foundElapseTime);

      var newElapse = new Date();
      newElapse.setTime(0);
      newElapse.setUTCHours(hrs);
      newElapse.setUTCMinutes(mins);
      newElapse.setUTCSeconds(secs);
      newElapse.setUTCMilliseconds(milli * 10); /* 3 digits in javascript Date object */
     
      UltraEdit.outputWindow.write( UltraEdit.document[timingDocix].selection +">>" + newElapse.toUTCString() );

      parsedElapseTimes.push( newElapse );
   }

   return parsedElapseTimes;
}


/* getDocumentIndex(filepath) */
/* If called without parameter = Find the tab index of the active document */
/* If called with parameter "filepath" = Find the tab index of that document. -1 = document not found */
function getDocumentIndex(filepath) {
   var tabindex = -1; /* start value */

   for (i = 0; i < UltraEdit.document.length; i++)
   {
      if(filepath) {
         if (UltraEdit.document[i].path.toLowerCase().indexOf(filepath.toLowerCase())!=-1) {
            tabindex = i;
            break;
         }
      }
      else {
         if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
            tabindex = i;
            break;
         }
      }
   }
   return tabindex;
}


(Hint: If you are having problems with your script: Start by running JavaScript Lint on it...)
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: add time from regex fields in script

Postby sklad2 » Sun Aug 10, 2008 10:18 am

Thank you for helping again. I like this alot.
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am


Return to Scripts