2

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
Kyle11
  • 23
  • 3
  • use [set /a](https://ss64.com/nt/syntax-replace.html) for arithmetic. – Stephan Jan 28 '19 at 20:21
  • Add `set $` to see that the [`for /L` loop](https://ss64.com/nt/for.html) assigns numbers `1..26` to letters `a..z`. You could use `set /A $A=1,$B=2,$C=3,$D=9,$E=4,$F=8` (etc.) instead to assign your own letter weights. Moreover, use `SET /a count=0,sum=0` and `SET /a count+=1,sum+=code`. – JosefZ Jan 28 '19 at 21:45

2 Answers2

2

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks for your help! One thing I noticed is when I type an input with spaces then just as many errors shows up so I extended your solution with part of John Kens's approach `set "text=!text: =!"` which solved this. May I ask a question about the output `%text%` variable? How can I add spaces between the characters so the `%text%` and `%nums%` variable can line up? – Kyle11 Jan 30 '19 at 13:54
  • Thanks! I did some experiments with it and start to get how this works! – Kyle11 Feb 07 '19 at 18:33
  • I suggest you to insert an `echo on` command before the interesting `echo !%out: =! !%!` line so you can see in the screen the exact code that is executed... – Aacini Feb 08 '19 at 02:42
1

Being that you want to set each letter to it's own value, extract the sum value from a new word, and display it, there are a few steps we need to take.

The first step will be to set the custom strings. For this a basic for loop can be used. Being that you quoted all your letters served a double benifite in that with the set command each result will be displayed as Set "A=1", Set "B=2", Exc.

For %%A in ("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") do (
    Set %%A
)

Keep in mind we are removing spaces in the sentence (If present) with set "text=!text: =!" or with syntax-replace.

The final step will to convert each letter to its corresponding value and add them all up. To do this we will have to pull each letter from the string. Using a loop we can pull these values and add them using Set /a "String=!String!+!New Number!".

GetSumUsingCustumNumericals.bat:

@Echo off
@setlocal ENABLEDELAYEDEXPANSION

Rem | Ask User For Word
Set /p "text=input: "
Set "orginaltext=!text!"
set "text=!text: =!"
Cls

Rem | Set Strings
For %%A in ("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") do (
    Set %%A
)

Rem | Convert Text
set pos=0
:NextChar
Set "Letter=!text:~%pos%,1%!"
Set "Converted=!%Letter%!"
Set "Numbers=!Numbers! !Converted!"
Set /a "Sum=!Sum!+!Converted!"
set /a pos=pos+1

if "!text:~%pos%,1!" NEQ "" goto NextChar
goto Finished

:Finished
Rem | Display Results
Echo Text: !orginaltext!
Echo Letter Values: !Numbers:~1!
Echo Sum: !Sum!

pause>nul
goto :EOF

Input:

Hello my name is John

Algorithm:

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

Output:

Text: Hello my name is John
Letter Values: 5 4 5 5 2 4 1 5 1 4 4 2 8 5 2 5 5
Sum: 67

For help on any of the commands do the following:

  • goto /?
  • set /?
  • for /?
  • if /?
  • So on.
John Kens
  • 1,615
  • 2
  • 10
  • 28
  • 1
    I don't understand why you "will have to also address a lowercase variable to the strings too" or to "convert the users varible string to all uppercase". Batch variables are _not_ case sensitive... – Aacini Jan 29 '19 at 04:12
  • There's no need to use `%%` or `!!` to reference variables in `set /A`, something like `set /A Sum=Sum+Converted` is sufficient and can even be simplified to `set /A Sum+=Converted`... – aschipfl Jan 29 '19 at 08:36
  • First, thanks for your help! Second, I tried to use this script within a loop for example: `goto :repeat` but the letter values of the previous calculation keeps getting summed up with each new one for some reason. – Kyle11 Jan 30 '19 at 12:13
  • @Kyle11: this is because, in despite of the extensive explanation and large code, the `Numbers` and `Sum` variables are not initialized... – Aacini Jan 30 '19 at 13:25