Is calculation possible (search/replace) ?

Help with writing and running scripts

Is calculation possible (search/replace) ?

Postby KOC2000 » Thu Jul 26, 2007 5:10 pm

Hi,
I just wanna know if this is possible
I've a text file with numbers, example

A=10
B=1
C=40

I wanna change these numbers, wanna add or subtract let's say 10% of each number.. is this possible with Ultraedit ?

Thank you.
User avatar
KOC2000
Newbie
 
Posts: 2
Joined: Wed Jul 25, 2007 11:00 pm

Re: Is calculation possible (search/replace) ?

Postby jorrasdk » Thu Jul 26, 2007 8:08 pm

No, it's not possible right out of the box.

If You are a version 13 user, you could create a script that takes the number in each line adds or subtracts and write the result back in the line. Here is an example. Change the math part in line 5 to fit your purpose:

Code: Select all
// Add a function to String object, that searches for numbers (decimal point = .)
// following an equal sign and multiply each with 1.1 (adds 10%) and round to integer.
String.prototype.addTenPctRound = function(){
   return this.replace(/[=]([\d.]+)/,function($0,$1) {
      return "="+(Math.round($1 * 1.1));   
   });
}

var lineno = 1;
UltraEdit.activeDocument.top();
while (! UltraEdit.activeDocument.isEof() ) { /* loop through the file */
   UltraEdit.activeDocument.selectLine();
   var line = UltraEdit.activeDocument.selection; /* read selection into variable */

   UltraEdit.activeDocument.write(line.addTenPctRound()); /* write line back into the editor */

   UltraEdit.activeDocument.gotoLine(++lineno,1);
}


Another option is to choose MS Excel instead since the text file is a simple delimited file (= is the delimiter). Use Excel's "text to column" function with '=' as the delimiter. Then use whatever formulas to calculate the new values. If you need the result back in the same format, use a formula to create the '='-delimited format in a new column:
=A1&"="&C1
which could be copy/pasted back into a text file.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Is calculation possible (search/replace) ?

Postby Mofi » Fri Jul 27, 2007 6:22 am

Additional info: When you switch to column mode Column - Column Mode, select the columns with the values and click on Column - Sum Columns/Selection UltraEdit will open a small window with the sum of the selected values. But this command cannot be used in macros or scripts.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Is calculation possible (search/replace) ?

Postby KOC2000 » Fri Jul 27, 2007 5:36 pm

Thank you so much.. gonna try and report back.
User avatar
KOC2000
Newbie
 
Posts: 2
Joined: Wed Jul 25, 2007 11:00 pm


Return to Scripts