I have created two sorted arrays of size n and m with numeric value in a batch file.
FOR /L %%a IN (0,1,!n!) DO ECHO !vector[%%a]!
FOR /L %%a IN (0,1,!m!) DO ECHO !vector2[%%a]!
This exactly shows the contents of my arrays.
Now I want to write a logic which will print merged sorted array.
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set i=0
set j=0
set /A totalElements =!n!+!m!
FOR /L %%A IN (1,1,!totalElements!) DO (
if !vector[!i!]! LSS !vector2[!j!]! (
echo "First list"
echo !%vector[!i!]%!
) else (
echo "Second List"
echo !%vector[!i!]%!
)
)
So, this if else logic is not working. Any idea where in the syntax I have gone wrong? I guess I am not extracting the value from the array correctly?
Incomplete analyse:
where are defined n and m variables in set /A totalElements =!n!+!m! ?
in !vector[!i!]!, batch parser is looking for variables !vector[! and !]! instead of nesting like in !vector[%i%]!;
!%vector[!i!]%! is totally unclear for me.
Your question seems to be too broad (a bit unclear aim) to give more positive and constructive notes.
Related
How can I make the sum of the imputed numbers given by the user from the keyboard ? I tried a for loop but is endless. I cannot figure out a break statement.
::#ECHO OFF
setlocal EnableDelayedExpansion
set /p X=
SET /A suma=0
:Loop
FOR %%G IN (%X%) DO (
IF %%G GTR 0 (
SET /A suma=%suma%+%%G
)
)
SHIFT
GOTO Loop
:completed
echo %suma%
What i want to do is to make the program more user friendyl like echo somthing for the user to know it is supposed to write somthing. I know the correct code for this question, but the numbers are given as parameters in the command (syntax: script.bat parameter1 parameter2 parameter3).
::#ECHO OFF
SET /A suma=0
:Loop
IF "%1"=="" GOTO completed
FOR %%F IN (%1) DO (
IF %1 GTR 0 (
SET /A suma=%suma%+%1
)
)
SHIFT
GOTO Loop
:completed
echo %suma%
Please give me any idea for the breaking statement in the first example or how can I modify the code to take the numbers from the users imput variable and not as paramaters.
Thank you.
SET /A suma=0
set "X=%*"
if not defined X set /p "X=No parameters detected "
if not defined X ECHO No user-input detected&goto completed
FOR %%G IN (%X%) DO (
IF %%G GTR 0 (
SET /A suma=suma+%%G
)
)
:completed
echo %suma%
%* means the command-line tail. If no tail, then ask user for input. If still no data, issue message and show result.
shift moves the command line tail one position, so %2 becomes %1, etc. You haven't provided a test to exit the routine - and the command-line tail is presumably empty, so you are shifting it (which does nothing) and looping.
The for...%%G.. processes the entire list %X%, so there's no point in the shift/loop - the data has already been processed.
I am writing a batch file with a for loop and am having trouble getting the left and mid logic working. I have successfully got this logic working outside of the for loop and have attached the code here.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a value = 1000
set /a range = "%value%"
set /a secrange = "%value%"
for /l %%x in (1, 1, 10) do (
echo %range%
set range=%range:~0,1%
set secrange=%secrange:~1,3%
echo !range!
echo !secrange!
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
The output is shown here.
Split 1000
So the above cmd bat is able to split the string and then combine it with a period mark in the middle of the two split strings.
However, in my for loop I don't achieve this result. I know it may be a syntax issue but I am not sure how to fix it (I have tried a lot of different things already).
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a range = "X"
set /a secrange = "X"
for /l %%x in (1, 1, 10000) do (
if %%x geq 1000 (
if %%x lss 10000 (
set "range=%%x"
set "secrange=%%x"
echo !range!
echo !secrange!
set "range=%range%:~0,1%"
echo !range!
echo %range%
set secrange=%secrange:~1,3%
echo !secrange!
echo %secrange%
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
REM End x greater 1000
)
REM End for loop
)
I use secrange the same way as I did in the original code without the for loop. I tried to manipulate the range portion with many different syntax combos but have not been able to output the left character 1. I only am able to output 0 for both characters.
I attached the output here. Bad Output
I understand there is an error (Echo is Off). Please ignore that it is just based off my echo output type for my personal testing.
The important two outputs are the third and fourth line (whichever one is referring to the correct syntax). Does anyone know how to fix the syntax in order to get the left character of 1000 in the for loop and the remaining 3 zeros of the 1000?
Is it possible to square the increment/step variable in a batch for loop.
This is what it looks like right now
FOR /L %%A IN (1024,1000, 1048576) DO (
do stuff
)
however instead of going up by 1000 each time, I want to go up by 2^10, 2^11 .... 2^20 (1048576) is it possible to do that?
No, for /l loops can not handle geometric increments. But you can use batch arithmetics for it
for /l %%a in (10 1 20) do (
set /a "A=1<<%%a"
setlocal enabledelayedexpansion
for %%A in (!A!) do ( endlocal
rem Your code here - do stuff
echo %%A
)
)
Delayed expansion is needed to handle the changing variable inside the block of code. If your inner code has no problems with delayed expansion being active, it can be simplified as
setlocal enabledelayedexpansion
for /l %%a in (10 1 20) do (
set /a "A=1<<%%a"
rem Your code here - do stuff
echo !A!
)
If switching to PowerShell is fine for you, you can use
foreach ($i in 1..3) {
# example output
echo "1000^$i = $([Math]::Pow(1024, $i))"
# start "myprogram.bat --parameter $x
& 'myprogram.bat' #("--parameter", [Math]::Pow(1024, $i))
}
I am writing a batch script as follows:
#echo off
setlocal enabledelayedexpansion
set Current_Node="Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)
Now,according to above code it should not go to the else part but in actual it is always entering to the else part.
I am not getting whether the problem is in syntax or the way i am comparing the strings.
So, please help me out.
You have too many quotes. Don't use quotes when setting the value or do not use quotes on "%Current_Node%".
When you use Current_Node="Node1", %Current_Node% will be equal to "Node1", but "%Current_Node%" will be equal to ""Node1"".
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
echo %Current_Node%
if "%Current_Node%" == "Node1" (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)
As suggested, there are quotes used where not necessary. I believe you need to use EQU instead of == etc. Try below if still having trouble.
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
if %Current_Node% EQU Node1 (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)
Double quotes are sometimes needed when setting a variable, so the solution can be simply to wrap the term in double quotes as seen in line two below. They do not become part of the variable.
#echo off
set "Current_Node=Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)
I need to make a for loop that sums up numbers from 1 to x where x is users input number.
I know how to make the for loop and display those numbers but I don't know how to sum them at the same time.
FOR /L %%x IN (1,1,%x%) DO ( echo %%x
)
to do arithmetics, use set /a. Also use delayed expansion to use the variable inside a block (between ( and ))
setlocal enabledelayedexpansion
set sum=0
FOR /L %%x IN (1,1,%x%) DO (
set /a sum=!sum!+%%x
echo + %%x = !sum!
)