0

I have array like a[0]= ABC , a[1]=ABC , a[2]=ABC and I want to compare it using :

if  %a[0]%==%a[1]%=%a[2]% ( echo Equal)

Comparing three is not working but comparing two works fine.

if  %a[0]%==%a[1]% ( echo Equal)

any suggestion, please ?

2
  • 2
    Where in the help file for the IF command did you read that you could use syntax like that for the IF command? Commented Jul 4, 2018 at 4:39
  • Related: Batch-file "and if" statement Commented Jul 4, 2018 at 6:07

2 Answers 2

1

To check multiple (pseudo-)array elements for equality, you could try this:

setlocal EnableDelayedExpansion
set "FLAG=#"   & rem // (flag that is going to be cleared in case of an encountered inequality)
set /A "MAX=9" & rem // (greatest array index number to be included in the comparison loop)
for /L %%I in (1,1,%MAX%) do if not "!a[0]!"=="!a[%%I]!" set "FLAG="
if defined FLAG echo All array elements from index 0 to %MAX% are equal.
endlocal
Sign up to request clarification or add additional context in comments.

Comments

0

A simple nested IF command will do what you want.

if  "%a[0]%"=="%a[1]%" if "%a[1]%"=="%a[2]%" echo Equal

Another simple solution for comparing 3 numbers to see if they are all the same.

if "%a[1]%,%a[2]%,%a[3]%"=="%a[2]%,%a[3]%,%a[1]%" echo All same

2 Comments

Hey this works.. but if I have a arry of 10 strings.. How can I compare them via for loop?
@ty139, what about extending that command line accordingly...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.