Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break inner loop in nested loop batch script

MY goal is to compare two files line by line and capture the changes. For that i am using two nested loops. I am stuck with braking the inner loop on some condition.

I am using label outside the inner loop for break it, but not working. It goes to label and terminate outer loop also.

@ echo off
SETLOCAL EnableDelayedExpansion
for /F "skip=8 tokens=* delims=." %%a in (sample.txt) do (for /F "skip=8 tokens=* delims=." %%b in (test.txt) do (if %%a==%%b (goto :next) else ( echo %%a) 
)
: Next
echo out of inner loop
)

Anyone can help....?

like image 365
Rock with IT Avatar asked Jan 17 '11 06:01

Rock with IT


People also ask

How do you break a loop out of a nested loop?

There are two steps to break from a nested loop, the first part is labeling loop and the second part is using labeled break. You must put your label before the loop and you need a colon after the label as well. When you use that label after the break, control will jump outside of the labeled loop.

How do you skip an inner loop?

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too.

How do I break out of multiple nested loops?

Break out of nested loops with a flag variableIn the condition that the inner loop ends with break , set the flag to True , and in the outer loop, set break according to the flag.

Is it possible to break out of nested loops in Java?

Java break and Nested LoopIn the case of nested loops, the break statement terminates the innermost loop. Here, the break statement terminates the innermost while loop, and control jumps to the outer loop.


2 Answers

A goto :label always breaks all loops.

But you can put your inner loop in a separated function, then it could work.

@echo off
SETLOCAL EnableDelayedExpansion
for /F "skip=8 tokens=* delims=." %%a in (sample.txt) do (
    call :myInnerLoop "%%a"
)

echo out of inner loop
)
goto :eof


:myInnerLoop
for /F "skip=8 tokens=* delims=." %%b in (test.txt) do (
    if "%~1"=="%%b" (
        goto :next
    ) else ( 
        echo %%a
    )
:next
goto :eof

One remark, breaking of FOR /L loops does not work as expected, the for-loop always count to the end, but if you break it, the execution of the inner code is stopped, but it could be really slow.

@echo ON
FOR /L %%n IN (1,1,1000000) DO (
  echo %%n - count
  goto :break
)
:break

EDIT:

Proof of concept

@echo off
SETLOCAL EnableDelayedExpansion
for %%a in (a b c) DO (
   echo Outer loop %%a
   call :inner %%a
)
goto :eof
:inner
for %%b in (U V W X Y Z) DO (
  if %%b==X (
    echo    break
    goto :break
  )
  echo    Inner loop    Outer=%1 Inner=%%b
)
:break
goto :eof

Output

Outer loop a
   Inner loop    Outer=a Inner=U
   Inner loop    Outer=a Inner=V
   Inner loop    Outer=a Inner=W
   break
Outer loop b
   Inner loop    Outer=b Inner=U
   Inner loop    Outer=b Inner=V
   Inner loop    Outer=b Inner=W
   break
Outer loop c
   Inner loop    Outer=c Inner=U
   Inner loop    Outer=c Inner=V
   Inner loop    Outer=c Inner=W
   break
like image 148
jeb Avatar answered Sep 30 '22 00:09

jeb


Even if the goto label is in the for loop, it also goes out the loop context. Such as

@echo off
for %%d in (A B) do (
    echo %%d
    for %%f in ( 1 2 ) do (
        goto loop
        :loop
        echo %%d %%f
    )
)

This will print out A %d %f

like image 24
user1037135 Avatar answered Sep 30 '22 00:09

user1037135