BATCH Scripting - Swapping array subscript values - arrays

I'd like to start this off by saying batch scripting is something I ever do, and it's for an assignment in my class, so please bear with me. I am trying to take an array index and swap one of the indices for another. If I echo out each index after assigning it I get the expected output, but if I try to echo the array, the array hasn't changed. As I said I am very new with programming and especially batch, so I'm sure there is something fundamental I am missing.
my output
if index[0] is GTR index[4] if I enter 5,4,3,2,1:
echo %index[4]% outputs 5 %index[0] outputs 1
echo %numbers% outputs 5,4,3,2,1
my code
#echo off
set /p num1=Enter first num
set /p num2=Enter second num
set /p num3=Enter third num
set /p num4=Enter foruth num
set /p num5=Enter fifth num
SET /a num1=%num1%
SET /a num2=%num2%
SET /a num3=%num3%
SET /a num4=%num4%
SET /a num5=%num5%
SET numbers=%num1% %num2% %num3% %num4% %num5%
(for %%x in (%numbers%) do (echo %%x))
echo my array %numbers%
if %num1% GTR %num5% (
SET /A temp=%num1%
SET numbers[0]=%num5%
SET numbers[4]=%temp%
echo INDEX 4 is: %numbers[4]% INDEX 1 is: %numbers[0]%
) else (
echo "end of array is greater than start"
)

As in an above comment I needed to use setlocal enabledelayedexpansion
after doing this I found it easier to assign each index separately, and I did not test if would work the way I had the code in my question. ie set /a value[1] = value[1]
#echo off
setlocal enabledelayedexpansion
SET /p elem[0]=Enter a num
SET /p elem[1]=Enter a num
SET /p elem[2]=Enter a num
SET /p elem[3]=Enter a num
SET /p elem[4]=Enter a num
rem CHANGE TO INTEGER
SET /a elem[0]=elem[0]
SET /a elem[1]=elem[1]
SET /a elem[2]=elem[2]
SET /a elem[3]=elem[3]
SET /a elem[4]=elem[4]
rem loop through each value 0-4, echo each index value
for /l %%n in (0,1,4) do (
echo !elem[%%n]!
)
rem Check if value of elem[0] greater than elem[4], if so swap their positions
if %elem[0]% GTR %elem[4]% (
set /a temp = elem[0]
set /a elem[0] = elem[4]
set /a elem[4] = temp
echo Element 0 is greater than element 4 the new array is:
for /l %%n in (0,1,4) do (
echo !elem[%%n]!
)
rem If not greater, no swap needed
) else (echo Element 0 is NOT greater than element 4 the array is still the same)

Related

How to increase alphabetic variable - Batch

I was able to increase the number variable:
SET /a Y=0
SET /a Y+=1
ECHO %Y% = 1
But I want to "increase" the variable with letters.
Ex:
SET Y=A
SET Y+=1
ECHO %Y% = B
Anyway to do something like this in Batch?
Only with a sort of fake pointer.
#Echo off
Setlocal EnableDelayedExpansion
Set "Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Set I=25
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
SET /A I+=1,I=I %% 26
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
To have the algorythm continue with A again after reaching Z, you've to calculate the modulus 26
Y=Z
Y=A

Batch script - Adding variable value to same variable

I'm trying to do something like this, but I can't make it to work.
for /F "tokens=1" %%a in (C:\Auxiliar4.txt) do (
if !counter! EQU 3 (set /A failed=%%a)
if !counter! EQU 9 (set /A completed=%%a)
if !counter! EQU 6 (set /A total=%%a)
set /A counter+=1
)
set /A counter=0
set /A failedday=%failedday%+%failed%
if %total% NEQ 0 (set /A success=(%completed%*100)/%total%)
My problem is with these two lines:
set /A failedday=%failedday%+%failed%
if %total% NEQ 0 (set /A success=(%completed%*100)/%total%)
The first issue is with variable "failedday", I need to read numeric values from a txt file, assign them to those variables (failed, completed, total) and then assign the value of "failed" to "failedday". Then another FOR loop starts to read a different txt with same format, assign those numeric values to "failed", "completed", "total" again (losing the previous values), and add new value of "failed" to "failedday" keeping the previous addition (example, filedday=2 after first "for", after second should be failedday=2 + New"Failed"Value).
The second issue is with "IF" structure. I need to calculate the average of completed transactions taking values from "For" loop, only if "total" is not equal to 0 (to avoid divide by zero), but when I add this line my script crashes.
Could anyone help me with this? Thanks!!!
Here's a previous StackOverflow question that asks the same sort of thing.
Here's another reference for doing arithmetic with DOS Batch files.
You need to enable delayed expansion to perform the arithmetic:
#echo off & setlocal EnableDelayedExpansion
set /A failed=0
set /A completed=0
set /A total=0
for /F "tokens=1" %%a in (C:\Temp\test.txt) do (
if !counter! EQU 3 (set /A failed=%%a)
if !counter! EQU 9 (set /A completed=%%a)
if !counter! EQU 6 (set /A total=%%a)
set /A counter+=1
)
echo Failed: %failed%
echo Completed: %completed%
echo Total: %total%
set /A counter=0
set /A failedday=failedday+failed
if total NEQ 0 (set /A success=completed*100/total)
echo Success: %success%

Using an if statement inside a for loop

I am trying to make a batch file to solve the first Project Euler problem, http://projecteuler.net/problem=1, but I need an if statement inside my loop to check if n modulo 3 or 5 is 0. And the sum has suddenly stopped working.
My code:
echo off
set sum=0
for /l %%n in (1,1,999) do (
set a/ sum+=%%n *(only add if n%%3 == 0 or n%%5 == 0)*
)
echo %sum%
pause
Here is a very efficient solution, though it is a bit obfuscated:
#echo off
setlocal
set /a sum=0
for /l %%N in (1 1 999) do set /a "sum+=%%N/!((%%N%%5)*(%%N%%3))" 2>nul
echo %sum%
The expression (%%N%%5)*(%%N%%3) yields zero if %%N is divisible by 3 or 5, or non-zero if it is not divisible by either. The ! takes the inverse logical value, so 0 becomes 1, and non-zero becomes 0. Dividing %%N by that expression yields either %%N or a division by zero error. So simply add that entire expression to the sum, and redirect error messages to nul.
Final result - only numbers divisible by 3 or 5 are added :-)
#ECHO OFF
SETLOCAL
set /A sum=0
for /l %%n in (1,1,999) do (
CALL :modulo %%n
IF DEFINED addme set /a sum+=%%n
REM CALL echo %%n %%sum%% %%addme%%
)
echo %sum%
GOTO :EOF
:modulo
:: set addme if %1 %% 3 == 0 or %1 %% 5 == 0
SET /a addme = %1 %% 3
IF %addme%==0 GOTO :EOF
SET /a addme = %1 %% 5
IF %addme%==0 GOTO :EOF
SET "addme="
GOTO :eof
Simply pass each value to the :modulo routine in turn and set a flag value to (clear or not)
OR
#ECHO OFF
SETLOCAL enabledelayedexpansion
set /A sum=0
for /l %%n in (1,1,999) do (
SET /a mod=%%n %% 3
IF NOT !mod!==0 SET /a mod=%%n %% 5
IF !mod!== 0 set /a sum+=%%n
rem CALL echo %%n %%sum%%
)
echo %sum%
GOTO :EOF
which does the same thing using delayedexpansion.
And the sum has suddenly stopped working.
I think your sum stopped working because your set needs to have the slash in front of the 'a', and not behind it, like this:
SET /A sum+=%%n
Also, there isn't an OR operator in DOS Batch, so you'll need to use a nested IF for that. This worked for me:
echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set sum=0
for /l %%n in (1,1,999) do (
SET /A mod3=%%n%%3
SET /A mod5=%%n%%5
IF !mod3!==0 (
SET /A sum+=%%n
) ELSE (
IF !mod5!==0 (
SET /A sum+=%%n
)
)
)
echo %sum%
ENDLOCAL
If you need more help, check out Rob van der Woude's Scripting pages. Specifically, here is a link to his page on performing mathematical operations in DOS batch files.

How to find large factorials using a batch script

#echo off
if %1.==. ( echo Missing parameter! Try passing the number as a parameter like 'factorial 10' without the quotes.
goto end )
setlocal enabledelayedexpansion
set /a count=0
set /a temp=0
set /a digits=1
set /a array1=1
for /L %%i IN (2,1,%1) do (
set /a temp=0
for /L %%j IN ( 1,1,!digits! ) do (
set /a temp=!temp!+!array%%j!*%%i
set /a array%%j=!temp!%%10
set /a temp=!temp!/10 )
set /a index=!digits!+1
for /L %%v IN (!index!,1,!index! ) do (
if !temp! NEQ 0 (
set /a array!index!=!temp!%%10
set /a temp/=10
set /a index+=1 ))
set /a digits=!index!-1)
for /l %%v IN ( !digits!,-1,1 ) do set array=!array!!array%%v!
echo !array!
echo Total # of decimal digits = !digits!
:end
pause
This is what i have gotten so far. It is quite stable under 10! but once i reach 15 or 20 it starts missing out a few digits.
My edit....
#echo off
if %1.==. ( echo Missing parameter! Try passing the number as a parameter like 'factorial 10' without the quotes.
goto end )
setlocal enabledelayedexpansion
set /a count=0
set /a temp=0
set /a digits=1
set /a array1=1
for /L %%i IN (2,1,%1) do (
set /a temp=0
for /L %%j IN ( 1,1,!digits! ) do (
set /a temp=!temp!+!array%%j!*%%i
set /a array%%j=!temp!%%10
set /a temp=!temp!/10 )
for /l %%v IN ( 1,1,30 ) do (
if !temp! neq 0 (
set /a digits+=1
set /a array!digits!=!temp!%%10
set /a temp=!temp!/10
)))
for /l %%v IN ( !digits!,-1,1 ) do set array=!array!!array%%v!
echo !array!
echo Total # of decimal digits = !digits!
:end
pause
Now i am forcing the the last inner loop to run 30 times eventhough temp would have been zero way before that. Is there any way to write a snippet that would be analogous to the following c code
while (temp)
{/code goes here/}
This execute only as long as temp is non zero.
Is there any partyicular limit to how big a variable can be in a batch?
I know it is a pain * to debug computational programs in a batch, I'm just trying to code this in all programming languages i know, Just so i could compare their speeds. Can somebody Please point out what i'm doing wrong here.
edit........22/12/13
#echo off
if %1.==. ( echo Missing parameter! Try passing the number as a parameter like 'factorial 10' without the quotes.
goto end )
setlocal enabledelayedexpansion
set /a count=0
set /a tempo=0
set /a digits=1
set /a array1=1
for /f "tokens=1-4 delims=:.," %%a IN ("%time%") do (
set /a "start=(((%%a*60)+1%%b %% 100)*60+1%%c %%100)*100+1%%d %% 100"
)
for /L %%i IN (2,1,%1) do (
set /a tempo=0
for /L %%j IN ( 1,1,!digits! ) do (
set /a tempo=!tempo!+!array%%j!*%%i
set /a array%%j=!tempo!%%10000
set /a tempo=!tempo!/10000 )
for /l %%v IN (1,1,2) do (
if !tempo! neq 0 (
set /a digits+=1
set /a array!digits!=tempo%%10000
set /a tempo=tempo/10000
))
)
for /l %%v IN ( !digits!,-1,1 ) do set array=!array!!array%%v!
echo !array!
echo Total # of decimal digits = !digits!
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /a "end=(((%%a*60)+1%%b %% 100)*60+1%%c %%100)*100+1%%d %% 100"
)
set /a elapsedcs=end-start
set /a elapseds=elapsedcs/100
set /a elapsedcs=elapsedcs%%100
echo %elapseds%.%elapsedcs% seconds
:end
rem label should never be the last statement
Is this what you meant aacini?
In order to achieve fast arithmetic operations on Big numbers in Batch, you must split the Bignum in groups of digits that can be managed via the 32-bits operations of set /A command. The maximum 32-bits signed integer is 2147483647, so the largest group of digits that can be multiplied this way is 4, because 5 digits (99999 x 99999) exceed the maximum number. Addition and multiplication must be achieved from right to left translating a "carry" to the next group to the left. Subtraction and division must be achieved from left to right translating a "borrow" to the next group to the right. The Batch file below use this method to succesively multiply a Bignum by a 4 digits factor, so it can calculate up to 9999! as long as all variables that contain the groups of 4 digits fits in the 64 MB size limit of the environment (look for "65,536KB maximum size" under "Setting environment variables"). The result is directly output to the screen in order to avoid the 8192 digits limit of one Batch variable.
EDIT: I slightly modified the program in order to run faster and get the number of digits in the result.
#echo off
if "%1" equ "" (
echo Missing parameter! Try passing the number as a parameter like 'factorial 10' without the quotes.
goto end
)
setlocal EnableDelayedExpansion
rem Calculate the factorial
set /A g1=1, groups=1
for /L %%n in (2,1,%1) do (
set carry=0
for /L %%g in (1,1,!groups!) do (
set /A group=g%%g*%%n+carry, g%%g=group%%10000, carry=group/10000
)
if !carry! neq 0 (
set /A groups+=1
set g!groups!=!carry!
)
)
rem Show the factorial
set /P "=!g%groups%!" < NUL
set /A groupsM1=groups-1
for /L %%g in (%groupsM1%,-1,1) do (
set group=000!g%%g!
set /P "=!group:~-4!" < NUL
)
echo/
rem Get the number of digits
set digits=0
for /L %%i in (0,1,3) do if "!g%groups%:~%%i,1!" neq "" set /A digits+=1
set /A digits+=4*groupsM1
echo Total # of decimal digits = %digits%
:end
pause
I had to rereread the code to see what it is doing. Nice.
You have to face three limits on your code.
1 - In the inner %%j and %%v loop, where buffer is used to multiply the current value in %%i, you face the limit indicated by Magoo. You can not operate with set /a with values greater than 2^31. As the values in the array are limited to 0-9, this means that this limit will not let you calculate factorials of numbers greater than 214748364 (aprox)
2 - There is a limit in the size of a environment variable. It can not hold more than 32767 characters. As you are concatenating the digits to output to console (the next limit is related), this limits you to factorials of numbers below 9273 (aprox).
3 - There is a limit in the length of the lines cmd can handle. It is 8191 characters. This does not limit your calc, but you can not use the method of concatenating in a variable to represent the number. If the method is not changed, this limits you to factorials of numbers below 2727 (aprox).
Batch is limited to signed-32-bit integers.
BTW - don't use temp or tmp as a user-variable. It is set by the system as a pointer to a directory where temporary files are stored.

Batch variable inside of variable

I've got a set of variables called p1 to p9
I've got 2 vars to set the range I'm interested in.
I want to list all the vars from minimum range +1 to the max.
It looks like that (and it won't work)
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END
The result I'd like to see:
fff
ggg
hhh
iii
this might work for you:
#ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
You need to Enabledelayedexpansion:
#echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal
And that should do what you want to do. Also to make a comment use a new line and type :: or REM.
Mona

Resources