<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.6.2. Calculating</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>
<BODY>
<h3>4.6.2. Calculating</h3>
<p>Of course, you can use external tools for calculations. All you need to do is to select the text which contains some mathematical expression and call NppExec's script which would allow to calculate it.</p>
<p>For example:</p>
<blockquote>
  <pre>set AWK_EXE = C:\tools\gawk\gawk.exe
&quot;$(AWK_EXE)&quot; &quot;BEGIN { print $(CURRENT_WORD) }&quot;</pre>
</blockquote>
<p>This simple script allows you to
    calculate
such expressions as &quot;1 + sin(3)/4&quot; or &quot;(2 + 0x10)*3&quot; etc.</p>
<p>You can modify the file &quot;do.awk&quot; from
    previous example <a href="4.6.1.html">[4.6.1]</a> in order to calculate an average value of numbers
given as the following table:</p>
<blockquote>
  <pre>1.  552
2.  554
3.  549
4.  551</pre>
</blockquote>
<p>Here is the AWK script (i.e. the
    content of &quot;do.awk&quot;) which calculates an
  average value of numbers in the second column:</p>
<blockquote>
  <pre>BEGIN {
   Sum = 0
   N = 0
}
NF == 2 {
   Sum += $2
   ++N
}
END {
   print &quot;avg = &quot; Sum/N &quot; (Sum = &quot; Sum &quot;, N = &quot; N &quot;)&quot;
}</pre>
</blockquote>
<p>Now, all you have to do is to select
    the given table (see above) in Notepad++ and execute NppExec's script &quot;do.awk on selected text&quot;.
  You will see the following output in NppExec's Console:</p>
<blockquote>
  <pre>avg = 551.5 (Sum = 2206, N = 4)</pre>
</blockquote>
<p>Of course, you can use other external tools for calculations of selected expressions or table data. The output of such external tools will be shown in NppExec's Console. Also you can redirect such output into a text file and then open that text file in Notepad++ <a href="4.5.html">[4.5]</a>.</p>
<p>NppExec ver. 0.4 introduces built-in calculator abilities provided by Function Parser (fparser). For example:</p>
<blockquote>
  <pre>// calculate math expression on the current line
// usage: type math expression in the editor and run this script
NPP_CONSOLE ?  // don't show the Console if it's hidden
SCI_SENDMSG SCI_GETCURLINE 100000 @&quot;&quot;  // get current line
set ans ~ $(MSG_LPARAM)  // try to calculate
SCI_SENDMSG SCI_LINEEND  // go to end of line
SCI_SENDMSG SCI_REPLACESEL 0 &quot; = $(ans)&quot;  // add the result
</pre>
</blockquote>
<p>Type &quot;help set&quot; for more details.</p>
<p>See also: Calculations, strlen and so on <a href="3.8.4.html">[3.8.4]</a>.</p>
</BODY>
</HTML>
