我有一组主要值和一个子集
主要设置:public static class ClassificationCounterReducer extends TableReducer<IntWritable,IntWritable,Put> {
子集:Group1,Group2,Group3
Group1_Sub1,Group1_Sub2,Group2_Sub1,Group3_Sub1,Group3_Sub2
对于每个主列表,我只想遍历其对应的子组列表并显示输出。
当前我正在使用以下代码
Group1 ->Group1_Sub1 and Group1_Sub2
Group2 ->Group2_Sub1
Group3 ->Group3_Sub1,Group3_Sub2
上面的代码会给我输出结果:
for %%s in (
Group1,Group2,Group3
) do (
echo set Main Group %%s >> Log.txt
for %%i in (
Group1_Sub1,Group1_Sub2,Group2_Sub1,Group3_Sub1,Group3_Sub2
) do (
echo Main Group is %%s and its sub group is %%i >>Log.txt
)
)
我想限制他们仅通过如下所示的相应列表
set Main Group Group1
Main Grpup is Group1 and its sub group is Group1_Sub1
Main Grpup is Group1 and its sub group is Group1_Sub2
Main Grpup is Group1 and its sub group is Group2_Sub1
Main Grpup is Group1 and its sub group is Group3_Sub1
Main Grpup is Group1 and its sub group is Group3_Sub2
set Main Group Group2
Main Grpup is Group2 and its sub group is Group1_Sub1
Main Grpup is Group2 and its sub group is Group1_Sub2
Main Grpup is Group2 and its sub group is Group2_Sub1
Main Grpup is Group2 and its sub group is Group3_Sub1
Main Grpup is Group2 and its sub group is Group3_Sub2
set Main Group Group3
Main Grpup is Group3 and its sub group is Group1_Sub1
Main Grpup is Group3 and its sub group is Group1_Sub2
Main Grpup is Group3 and its sub group is Group2_Sub1
Main Grpup is Group3 and its sub group is Group3_Sub1
Main Grpup is Group3 and its sub group is Group3_Sub2
我该如何实现?
答案 0 :(得分:1)
在内部(%% i)循环内:
ECHO %%i|FINDSTR /b /i /L /c:"%%s_">nul
IF NOT ERRORLEVEL 1 echo Main Group is %%s and its sub group is %%i >>Log.txt
将Group1_Sub2
回显到findstr
中,该{p}查找以/b
当前值+下划线开头的字符串%%s
。 /i
表示不区分大小写,/L
表示文字比较,/c:
表示要检测的字符串。
如果findstr
找到了要查找的字符串,则errorlevel
将被设置为0或1。 >nul
禁止输出。然后可以使用常规语法测试errorlevel
。
答案 1 :(得分:0)
当您谈论变量时:以下代码自行查找主要组及其子组:
@echo off
setlocal enabledelayedexpansion
REM following line just for generating test variables:
for %%a in (1 2 3) do for %%b in (1 2 3 4) do set "Group%%a_Sub%%b=!random!"
REM search MainGroups:
for /f "delims=_" %%a in ('set group') do set "Main_%%a=Group"
REM process each MainGroup
for /f "tokens=2 delims=_=" %%a in ('set Main_') do (
echo set Main Group %%a
for /f "delims==" %%b in ('set %%a_') do (
echo Main Group is %%a and its sub group is %%b and its content is !%%b!
)
)
专业版:无需对每个变量名称进行硬编码
骗局(?):不会列出空的(不存在的)变量(取决于您的需求,甚至可能是另一个Pro)