Batch function help?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Batch function help?

#46 Post by pieh-ejdsch » 11 Jul 2014 11:40

this Routine I have optimized.

1.
Same Letters Upper- and Lowercase can Set into one Var (named Letter).
once a time to intitiate this Alphabet.

2.
only one Forloop to read the string one by one.

3.
with the correct "delims" (all other Singn) we reduce the nonAlphabetic Output and Error Message when Calculate.

Sorry i forgot the Code

Code: Select all

@echo off
set t0=%time%

:: initiate Alphabetical List
for %%h in (A E I L N O R S T U) do set /a %%h=1
for %%h in (D G) do set /a %%h=2
for %%h in (B C M P) do set /a %%h=3
for %%h in (F H V W Y) do set /a %%h=4
set /a K=5
for %%h in (J X) do set /a %%h=8
for %%h in (Q Z) do set /a %%h=10

call :scrabbleScore "This test case should return sixty-six points" score
echo Phrase: This test case should return sixty-six points
echo Score: %score%
echo(

set "myString=This phrase is supposed to be worth exactly one hundred twenty points in Scrabble"
call :scrabbleScoreVar myString score
echo Phrase: %myString%
echo Score: %score%
call :difftime "%t0%" "%time%" t1

pause >nul
exit /b

:scrabbleScore phrase score -- Calculates and returns the Scrabble score of a phrase in quotes
::                          -- phrase [in]: phrase to calculate score of (must be in quotes)
::                          -- score [out]: variable name to store result to
if "%~2" neq "" set ?=%1&call :scrabbleScoreVar ? %~2
exit /b

:scrabbleScoreVar phrasevar score -- Calculates and returns the Scrabble score of a variable
::                                -- phrasevar [in]: variable name to calculate score of
::                                -- score [out]   : variable name to store result to
setlocal disabledelayedexpansion
set/a $=0
for /f delims^=^^1234567890!^"§$%%^&^/^(^)^=^?^\^.^+^-^_^#^'^*^~^´^`^<^>^|^@^{^[^]^}^:^;^   ^  %%X in (
 '"cmd /u /v /c echo(!%~1!|find /v "" "'
) do 2>nul set /a $ +=%%X
endlocal&if "%~2" neq "" set %~2=%$%
exit /b

:difftime
  setlocal
   set t0=%~1
   set t1=%~2
   for /F "tokens=1-8 delims=:.," %%a in ("%t0: =0%:%t1: =0%") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
   echo %t0% - %t1% elapsed %a% cs
  endlocal & set %3=%a%
exit /b


Phil

Post Reply