我使用ant替换url中的值:
<property name="dir" value="dir1, dir2, dir3" />
<property name="files" values="file1, file2, file3" />我希望将url中的值替换为dir1、file1然后是dir2、file2然后是dir3、file3。我循环两次来替换,但不是打印三次和替换所有的值,它替换和打印6次。
这里是我的代码:
<target name="test">
<foreach param="dirval" in="${dir}">
<foreach param="filesval" in="${files}">
<sequential>
<echo message="Testing structure: ${dirval}/${filesval}" />
</sequential>
</foreach>
</foreach>预期输出:
Testing structure: dir1/file1
Testing structure: dir2/file2
Testing structure: dir3/file3但got:
Testing structure: dir1/file1
Testing structure: dir1/file2
Testing structure: dir1/file3
Testing structure: dir2/file1
Testing structure: dir2/file2
Testing structure: dir2/file3
Testing structure: dir3/file1
Testing structure: dir3/file2
Testing structure: dir3/file3发布于 2015-03-24 21:22:22
之所以有此输出,是因为您处于一个由3个元素组成的双foreach循环中,因此您可以循环并打印结果9次,而不是所需的3次。( dir上的foreach循环,文件上的3次*foreach循环,3次)(正如您在当前输出中看到的那样)
我不知道Ant,但在Java中,您要完成的任务如下所示。(以获得所需的结构或输出)
只使用一个循环:
string dir[] = {"dir1","dir2","dir3"};
string files[] = {"file1","file2","file3"};
for (int i = 0; i < dir.length, i++){
System.out.println("Testing structure: " + dir[i] + "/" + file[i])
}发布于 2015-03-25 19:00:38
下面的代码使用第三方蚂蚁-对照库。Ant-cont肋骨提供了几个允许我们模拟数组的任务:
<project name="ant-foreach-array" default="run">
<!-- Ant-Contrib provides <var>, <for>, <math>, and <propertycopy>. -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<target name="run">
<property name="dirs" value="dir1,dir2,dir3" />
<property name="files" value="file1,file2,file3" />
<var name="fileIndex" value="1" />
<for list="${files}" delimiter="," param="file">
<sequential>
<property name="file.${fileIndex}" value="@{file}" />
<math
result="fileIndex" datatype="int"
operand1="${fileIndex}" operation="+" operand2="1"
/>
</sequential>
</for>
<var name="dirIndex" value="1" />
<for list="${dirs}" delimiter="," param="dir">
<sequential>
<propertycopy
name="fileIndex"
from="file.${dirIndex}"
override="true"
/>
<echo message="Testing structure: @{dir}/${fileIndex}" />
<math
result="dirIndex" datatype="int"
operand1="${dirIndex}" operation="+" operand2="1"
/>
</sequential>
</for>
</target>
</project>结果:
run:
[echo] Testing structure: dir1/file1
[echo] Testing structure: dir2/file2
[echo] Testing structure: dir3/file3https://stackoverflow.com/questions/29242976
复制相似问题