Math and Notepad++
-
Talking about FAQ: Can I do a Mathematical Replacement,
@freezer2022 said in Is it possible to do automatic mathematical operations in specific lines in Notepad++?:
Maybe in that FAQ, apart of Columns++ plugin, it’s worth to mention another 2 math related plugins: Expression calculator and NppCalc :
I moved my reply here, because the focus of your reply there was on the FAQ, not about the question you were replying to. (If you want to talk about them, and other math-capable plugins, feel free to do so here, rather than cluttering that specific question.)
As far as I have ever been shown, neither of those plugins you mentioned can perform search-and-replace-with-math, which is the point of the FAQ. Just mentioning other, non-search-and-replace plugins that happen to do math seems off-topic for that FAQ, to me.
Also, I don’t think the FAQs should be recommending plugins that are 32-bit only, nor ones that aren’t at least relatively recent. A lot of plugins that are as old as the two you linked are out-of-date enough that they aren’t using the right versions of plugin API calls, and can actually crash Notepad++ under certain circumstances. (And the API calls affected by such in the last few years include ones that read or write text from and into the active editor text… which is exactly what both of the plugins you mentioned do.)
-
@Thomas-Knoefel said in Is it possible to do automatic mathematical operations in specific lines in Notepad++?:
@freezer2022
I can provide you with an additional solution using the MultiReplace Plugin available in the upcoming Notepad++ version 8.5.8.
With this, you’ll have an even richer pick of solutions to choose from, on top of the already shared ones ;-)I recently asked @Coises to provide some Columns++ examples to match up with the PythonScript solutions, in the Problems with Math FAQ’s Columns++ entry.
If you wouldn’t mind doing the same thing for MultiReplace Plugin in reply here, I can update the FAQ to include your plugin in the list of options in the FAQ as well.
-
JsonTools can perform search-and-replace-with-math in anything that is reasonably close to the JSON specification (see documentation for some of the deviations it can tolerate), plus INI files and some other stuff.
Thinking about adding support for other types of documents, e.g. TOML, but not sure when that will happen.
Since JSON is incredibly frustrating to parse with regular expressions, JsonTools is a reasonable option so long as you keep its limitations in mind.
-
@PeterJones
This is in a nutshell how MultiReplace is working for Math.Activating ‘Use Variables’:
This option facilitates math functions and dynamic string substitutions within a Replace String. It can work with all search settings combined and is not exclusively dependent on regex.Commands Overview:
Eitherset
orcond
command is mandatory to set in ‘Replace with’ to wrap the math functions.
–set(strOrCalc)
- simple push of math results to Replace
–cond(condition, trueVal, [falseVal])
- if-then or if-then-else condition for push of resultsExamples of Commands Usage:
–set("replaceString"..CNT)
- results in “replaceString3” (assuming CNT = 3).
–set(LINE+5)
- results in “10” (assuming LINE = 5).
–cond(LINE<=5 or LINE>=9, "edge", "center")
- results in “edge” (assuming LINE = 5).
–cond(LINE<3, "Modify this line")
- Keeps original text if condition is false (assuming LINE >= 3).Variables Overview:
(will be enhanced in future by User requests):
–CNT
: Count of the detected string.
–LINE
: Line number where the string is found.
–APOS
: Absolute character position in the document.
–LPOS
: Relative line position.
–LCNT
: Count of the detected string within the line.
–COL
: Column number where the string was found (CSV-Scope option selected).
–MATCH
: Contains the text of the detected string.Special Variables:
CAP1, CAP2
, …: Correspond to regex capture groups $1, $2. CAP variables are usable for calculations or as strings.Format Handling: If the CAP variable is a number it can interpret both dot and comma as decimal separators, making international usage more straightforward. Thousand separators are not supported.
Note: $1 and $2 can be used in ReplaceString but content will only be resolved in CAP Variables for conditions and math functions
Example of special Variables:
Find:(\d+.\d+)
Replace with:set(CAP1 * 1.2)
- multiplies decimal number with 1.2Basic rules:
– Strings have to be quoted by'
or"
– Concatenation is by..
– Arithmetic:+, -, *, /, ^, %
– Relational:==, ~=, <, >, <=, >=
– Logical:and, or, not
Example of combining:
Find:Price: (\d+(\.\d+)?)
Replace with:cond(CAP1~=0, "Price per unit: " ..(CAP1/10), "Price cannot be zero")
- Calculates Price per Unit if not Zero.Further functionality:
As the engine is LUA driven all LUA math and string functions can be used in set or either condExample with advanced math Function:
Find:Radius: (\d+)
Replace with:set("Circumference: "..(2 * math.pi * CAP1))
- Calculates the circumference of a circle given its radius.Example with a string function for alignment:
Find: ;
Replace with:cond(LCNT == 1, string.rep(" ", 20 - (LPOS))..";")
- aligns first occurrence of semicolon in Line to the 20th character position. -
@PeterJones said in Math and Notepad++ (…)
The same reply: LINK applies here as well, I’ll leave you now guys, continue your discussion.