There are built in functions that make reversing a string simple enough that you can embed the functions directly within the replace string.
mirror.bat
Code: Select all
@call jrepl ".+" "$0.split('').reverse().join('')" /j /f input.txt /o mirror.txt
reverse.bat
Code: Select all
@call jrepl "\w\w+" "$0.split('').reverse().join('')" /j /f input.txt /o reverse.txt
Randomly scrambling a string is more complex. In this case it makes sense to define a scramble function and then use it within the replace string. One option for defining a custom function is to use /JBEG.
jumble.bat via /JBEG
Code: Select all
@call jrepl "(\w\w+)(\w)" "scramble($1)+$2" /j /f input.txt /o jumble.txt ^
/jbeg "function scramble(s){s=s.split('');for(var i = s.length,j,k;i;j=parseInt(Math.random()*i),k=s[--i],s[i]=s[j],s[j]=k); return s.join('');}"
That looks pretty ugly. There are ways to clean it up, but there is an easier way that is also reusable. You can create a file containing one or more functions and include the definition using /JLIB.
scramble.jlib
Code: Select all
function scramble(s) {
s=s.split('');
for(
var i=s.length, j, temp;
i;
j=parseInt(Math.random()*i), temp=s[--i], s[i]=s[j], s[j]=temp
);
return s.join('');
}
jumble.bat via /JLIB
Code: Select all
@call jrepl "(\w\w+)(\w)" "scramble($1)+$2" /j /jlib scramble.jlib /f input.txt /o jumble.txt
Now for the tricky part - producing all 4 results in one pass as you have shown in your sample output. I got creative on this
Have fun figuring out how it works
combined.bat (reusing scramble.jlib)
Code: Select all
@echo off
setlocal
set "rev=split('').reverse().join('')"
>combined.txt (
echo normal,jumble,mirror,reverse
echo(
call jrepl "(\w*)(\w)|\W+"^
"rev+=$0.%rev%;scramble($2)+$3|rev+=$0,$0"^
/jbegln "norm=$txt; rev=''"^
/jendln "$txt=norm+','+$txt+','+norm.%rev%+','+rev"^
/jlib scramble.jlib /j /t "|" /f input.txt
)
Dave Benham