I need to write a script, in.bat, where
- the user inputs a 3 digits number (as a string) i.
- the script has 4 predefined groups of numbers, number from 1 to 4
- the script tests membership for i in the groups, and return the index of the group containing i.
I’m not familiar with declaring and initializing any kinds of data structure (e.g. list, arrays and the like) for batch files, so can someone help me on this?
Pseudo code:
::Returns 1,2,3,4,5 Depending on testNum passed
group1= <822-824,829,845,851,859,864,867>
group2= <826-828,830-839,843-844,847-850,852-854,860-862,883>
group3= <855-858,861,863,865>
group4= <877-882,884>
if %1 is member of group1
return 1
if %1 is member of group2
return 2
if %1 is member of group3
return 3
if %1 is member of group4
return 4
Thank you!
Batch does not have any formal complex data structures like arrays, lists, or objects. But you can emulate them. Here is an efficient solution that defines the groups with nearly the format as in your question.
The above solution is fine for occasional calls. But if you were going to call the routine many thousands of times, then it would be better to initialize an array of valid values with assigned group numbers. Then each test becomes a direct read of the value, instead of having to call a routine. However, it is possible to abuse this technique. Assign enough values and each variable assignment gets slower and slower. You might also spend more time setting up the array than actually testing values.
Note there is no significance to the characters
[].in the variable names. They could be stripped out of the variable names and the code would function the same. They are there only to aid in the understanding the intent of the variables.