Fun with REPL.BAT JScript expressions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Fun with REPL.BAT JScript expressions

#1 Post by dbenham » 08 Nov 2014 14:56

The new REPL.BAT v6 J option allowing the replacement value to be specified as a JScript expression provides for some interesting applications.

Convert text to upper case

This is downright trivial.

upper.bat

Code: Select all

@repl ".+" "$0.toUpperCase()" j

Sample usage:

Code: Select all

C:\>echo HELLO world!|upper
HELLO WORLD!


Convert text to lower case

Another trivial example

lower.bat

Code: Select all

@repl ".+" "$0.toLowerCase()" j

Sample usage:

Code: Select all

C:\>echo HELLO world!|lower
hello world!


Convert text to mixed case (initial caps)

This is a bit more interesting.

initCaps.bat

Code: Select all

@repl "(\b.)|.*?\b" "$1?$0.toUpperCase():$0.toLowerCase()" j

Sample usage:

Code: Select all

C:\>echo HELLO world!|initCaps
Hello World!


ROT13 cipher

It is simple to implement the classic symmetric rotation cipher that can be used to both encode and decode a message.

ROT13.BAT

Code: Select all

@repl "[a-z]" "var c=$0.charCodeAt(0), off=c>=97?97:65; String.fromCharCode((c-off+13)%%26+off)" ij

Sample usage:

Code: Select all

C:\>echo Hello world!|rot13
Uryyb jbeyq!

C:\>echo Uryyb jbeyq!|rot13|rot13
Hello world!


English to Pig Latin translator

Now this is fun :D

This fairly sophisticated implementation has the following features:
- If word has initial capitalization, then so does translation, else each letter's case is preserved
- Properly handles contractions
- Treats hyphenated words as two words
- Properly handles Y, even in a word like Yttrium (a rare earth metal)
- Properly handles Qu
- Does not translate "words" containing any numeric digits

PigLatin.bat

Code: Select all

@repl "\b(?:((?:y(?=[aeiou])|qu|[bcdfghjklmnpqrstvwxz])(?:qu|[bcdfghjklmnpqrstvwxz])*)+([a-z']*)|([a-z']+))(?![a-z\d])" ^
"var out=$3?$3+'way':$2+$1+'ay';^
$0.substr(0,1).toUpperCase()+$0.substr(1).toLowerCase()==$0?out.substr(0,1).toUpperCase()+out.substr(1).toLowerCase():out" ij

Sample Usage:

Code: Select all

C:\>echo Hello world!|pigLatin
Ellohay orldway!


Dave Benham

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Fun with REPL.BAT JScript expressions

#2 Post by Aacini » 08 Nov 2014 23:04

Yes, "you may even introduce a much more complex JScript replacement expression that include several assignments (to auxiliary variables) via comma JScript operator", so complete algorithms can be assembled in the form of a long expression. The JScript Conditional Operator (?:) is the key to assemble an "If-Then-Else" structure in an expression, but even more complex structures can be used via auxiliary functions that allows to "expression-ize" a code block. For example:

Code: Select all

function FOR (init,test,inc) {var Ans=0; for ( eval(init) ; eval(test) ; eval(inc) ); return Ans};

If previous line is included in the JScript program (REPL.BAT or FindRepl.bat, etc), the replacement expression may use the FOR function using submatched subexpressions (or any other value) as parameters. A simple example:

Code: Select all

type filename.txt | FindRepl "for\s+\(\s*(\S+)\s*;\s*(\S+)\s*;\s*(\S+)\s*\)\s*(\S+)\s*;" "FOR($1,'Ans+=$4,$2',$3)" /J

Other structures may be defined and used this way (like While-Do); just a standard must be established in order to define the way that the "structured-functions" will be used in the replacement expression.

The JScript program may also define a set of variables designed to be used in the replacement expression. For example, if this line is included in the JScript program:

Code: Select all

var Aux1=0, Aux2=0, Aux3=0;

... then these variables could be used in the replacement expression this way:

Code: Select all

type filename.txt | FindRepl "(Data: \d+)|(Total: \d+)|(Average: \d+)"
                             "$1?Aux1+=$1.substr(6),Aux2+=1,$1:($2?'Total: '+Aux1:'Average: '+(Aux1/Aux2))" /J

Previous command would find all numeric data in the file preceded by "Data: ", accumulate they and replace the correct values in the (last) lines preceded by "Total: " or "Average: ".

The replacement expression could also define/create elements of an array that can be processed after the last line of the file to produce any desired result. In order to made this feature easier to use, a new option could be added to the JScript program to define an additional expression that will be executed and displayed after the last line of the file; for example: /L:"'Total: '+Aux1+", Average: '+(Aux1/Aux2)"

This way, the JScript program (REPL.BAT or FindRepl.bat) can be used as a general-processing program capable to solve any particular problem, because the particular code required for each problem can be injected in the replacement expression.

Antonio

Post Reply