I found a similar topic and solution by Endoro about converting letters into their numerical counterpart, but in my case I want to use a predefined set of numbers then sum them up with each other. Is that possible?
What I meant by that:
"A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
The output should look something like this:
Atom
1 7 2 4
14
The above mentioned reference:
@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET /p "text=input : "
cls
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
FOR /l %%x IN (1,1,26) DO SET "$!alfa:~%%x,1!=%%x"
SET /a count=0
:loop
SET "char=!text:~%count%,1!"
SET "code=!$%char%!
SET /a count+=1
IF DEFINED char SET "line=!line!%code% "&GOTO :loop
ECHO %text%
ECHO %line%
I'm not quite familiar with the full extent of the for
command so an explanation of the solution would be appreciated.
I want to understand how this works. Thanks in advance.
There are several different ways to solve the same problem. This solution is very simple and it does not require a single for
command! Each simple step is explained in the code. Perhaps the "most complicated" :/
part is the initialization of the variables that have the values of the letters.
This line:
set init="A=1" "B=2" "C=3" ... "X=5" "Y=1" "Z=1"
is exactly the same line of "initial values" you posted in the question.
This line:
set %init: =&set %
is a simple replacement that change each space by &set
. This means that previous line is changed to:
set "A=1"&set "B=2"&set "C=3" ...&set "X=5"&set "Y=1"&set "Z=1"
and, after that, the line is executed... Simple, isn't it? ;)
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
set "nums="
set "sum=0"
set /P "text=input: "
echo %text%
:loop
rem Get first char in text
set "char=%text:~0,1%"
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %nums%
ECHO %sum%
Example:
input: Atom
Atom
1 7 2 4
14
The output example given your input example is exactly the same you listed above...
EDIT: New version added
@echo off
setlocal
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize variables
set %init: =&set %
:repeat
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
set "out="
set "nums="
set "sum=0"
:loop
rem Get first char in text and join it to out
set "char=%text:~0,1%"
set "out=%out%%char% "
rem Get code and add it to sum
set /A "code=%char%, sum+=code"
rem Join code to nums
set "nums=%nums%%code% "
rem Remove first char from text and repeat
set "text=%text:~1%"
IF DEFINED text GOTO :loop
ECHO %out%
ECHO %nums%
ECHO %sum%
echo/
goto repeat
Example:
input: Atom
A t o m
1 7 2 4
14
input: My name is Antonio
M y n a m e i s A n t o n i o
4 1 5 1 4 4 2 8 1 5 7 2 5 2 2
53
input:
EDIT: Another shorter version, just for fun...
@echo off
setlocal EnableDelayedExpansion
rem Define initialization string
set init="A=1" "B=2" "C=3" "D=9" "E=4" "F=8" "G=2" "H=5" "I=2" "J=5" "K=2" "L=5" "M=4" "N=5" "O=2" "P=8" "Q=1" "R=9" "S=8" "T=7" "U=6" "V=6" "W=1" "X=5" "Y=1" "Z=1"
rem Initialize one-letter variables
set %init: =&set %
:repeat
rem Read a line from user
set "text= "
set /P "text=input: "
set "text=%text: =%"
if not defined text goto :EOF
rem Insert a space between letters
set "out="
:loop
set "out=%out% %text:~0,1%"
set "text=%text:~1%"
IF DEFINED text GOTO :loop
set "out=%out:~1%"
rem Get the sum: replace the space between letters by a plus sign
set /A "sum=%out: =+%"
rem Show letters separated by space
echo %out%
rem Change spaces by "! !" to show *the values* of the letters via Delayed Expansion
echo !%out: =! !%!
echo %sum%
echo/
goto repeat