Sid ABS
Sid ABS

Reputation: 59

Splitting a string and loop through the variables

I am having a string of format 101514,101234,101645,101798. I want to split the string on , and then loop through each of the individual elements comparing that element with the required value.

set required_value=1024
for /f  "delims=," %%a in ("101514,101234,101645,101798") do (
    set found=%%a
    if "%found%"=="%required_value%" (
        echo true
    ) else (
        echo false
    )
)

Here obviously, only first part is considered. I can use "tokens=1,2,3" and then %%a, %%b, %%c so on to compare, but the length is not fixed.

Is there any other way to solve it.

Upvotes: 2

Views: 80

Answers (4)

Mofi
Mofi

Reputation: 49086

I suggest a different method without a loop.

@echo off
setlocal EnableDelayedExpansion
set "RequiredValue=1024"
set "ValuesList=101514,101234,101645,101798"
rem Prepend and append a comma on value list.
set "AllValues=,%ValuesList%,"
rem Use substring replacement to find out if the required
rem value is included in list of values or is missing in list.
if "!AllValues:,%RequiredValue%,=!" == "%AllValues%" (
    echo %RequiredValue% not found in %ValuesList%
) else (
    echo %RequiredValue% is included in %ValuesList%
)
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

Upvotes: 4

Aacini
Aacini

Reputation: 67216

The obvious solution is to use a plain for command (no /F switch) as suggested by wOxxOm.

A more efficient method is to replace the required value from the list, as Mofi described in his answer.

There is another method that only works when the values are numbers. The trick consist in assemble an arithmetic expression that subtract the required value from each one of the values in the list, and then multiply all partial results; if the required value is one of the list, the result is zero.

@echo off
setlocal

set "ValuesList=101514,101234,101645,101798"

rem Create the test function
set "IsValueInList(x)=(%ValuesList:,=-x)*(%-x)"

rem Test if a value is in list
set "RequiredValue=1024"
set /A "test=%IsValueInList(x):x=RequiredValue%"

if %test% equ 0 (
   echo true
) else (
   echo false
)

This program uses two interesting tricks: the definition of a Batch "function" as described at this post, and a special way to "split a string on delimiter" as explained here.

Upvotes: 2

aschipfl
aschipfl

Reputation: 34899

An alternative method is using call to run a subroutine, which uses shift:

set required_value=1024
set list_of_values=101514,101234,101645,101798

call :SUBSPLIT %list_of_values%
echo %found%
exit /B

:SUBSPLIT
set found=false
:LOOP
if "%1"=="" (
    exit /B
) else if "%1"=="%required_value%" (
    set found=true
    exit /B
)
shift
goto :LOOP

Type call /? and shift /? in the command prompt to get mote information about the core commands of this approach.

Upvotes: 1

woxxom
woxxom

Reputation: 73526

  • use plain for without /f "delims"
  • don't quote the string
  • don't assign to found variable, it won't work without delayed expansion inside the loop, use %%a

for %%a in (101514,101234,101645,101798) do (
    if "%%a"=="%required_value%" (
        echo true
    ) else (
        echo false
    )
)

Upvotes: 2

Related Questions