1.

Solve : Setting variable in nested loops?

Answer»

Hi,

Does setting variable within nested for and if loops is an issue.

I was trying the above thing, but the variables don't get set.

ThanksYou might have mentioned your OS and given an example of your code. Try CALLING the set statement. (ex: call set /a idx=%%idx%%+1 ).

Nesting IF and FOR statments adds a level of complexity which makes debugging difficult. Batch coding is not a programming language. Use a Windows Script which has constructs for just this purpose.

Hope this is what you're looking for.

8-)I am using XP.

Yesterday i got stuck in a very disgusting problem.

I am giving my code here

for /F "tokens=2 delims=:" %%a in ('cleartool pwv ^| grep -i "Working directory view" ') do (
for /f "tokens=1-4 delims=_" %%i in ('echo %%a ^| sed -e "s/ //g" ') do (
if "%CCASE_TOOL_SERVER%" == "bombld" (
set Image_Loc=\\pnqfiler3\rte\images\working\%%i\%%l
echo %Image_Loc%
)
echo %Image_Loc%
)


Here the last if statement is not setting the variable.
If i put a echo "set ..." the WHOLE thing is perfect.
It just doesn't get set.
Everywhere i echo the varaibel it just "echo on/off", as the variable is not set.Calling the set statement doesn't also help the proceedings.

Same result. It worked on breaking the loop

But again the same thing, WHY Quote

Yesterday i got stuck in a very disgusting problem.
Imagine how the interpreter feels.

From experience, the best way to debug batch files is one line at a time using ECHO to check the validity of the variables as you go.

Code: [Select]for /f "tokens=2 delims=:" %%a in ('cleartool pwv ^| grep -i "Working directory view" ') do echo %%a

Once you satisfied that %%a has a valid value you can move on:
Code: [Select]for /f "tokens=1-4 delims=_" %%i in ('echo %%a ^| sed -e "s/ //g" ') do echo %%i %%J %%k %%l

Specific to your code, where was CCASE_TOOL_SERVER set? Being inside a FOR, try using double % around the CCASE_TOOL_SERVER.

Unless you are getting paid by the word, there are any number of script languages (vbscript, jscript, perl, python, rexx) that would make your life easier. All but Rexx are free and vbscript and jscript are already installed on Windows MACHINES. Use the best tool for the job.

Good luck. 8-)

I don't have some of these utilities (MKS Toolkit :-?) on my system so I couldn't test your code.To test it you don't need any utilites,

Just put anything in the for loop and if statement.

The biggest dilema came in the line of set:

I tried:
echo set ...
This was perfect.

The only problem was it didn't get registered.
What i am guessing right now is that there is an issue with variables when the nesting is big.

I had a similar issue in one more of my mails.Quote
To test it you don't need any utilites, Just put anything in the for loop and if statement.

Not quite true, as batch language is largely dependent on the data and the FOR instruction utilizes patterns when parsing. Unless I lost my ability to count, are you not missing a close paren at the end of your code?

Assuming %%a and %%l have valid values and the IF statement is true, try calling the SET statement. No guarantees of course, but sometimes trial and error is the best way to go.

I'm still unclear why you find it necessary to create such complex nested IF's and FOR's. I've always found the Keep It Simple Stupid (KISS) method to be quite helpful. The goal is to produce results, not choke the interpreter.

Good luck. 8-)



Yes,

From now onwards i am trying to do without nested loops.

By the way, I call it Keep it Short & Simple ( My KISS )


Discussion

No Comment Found