使用固定数量的迭代迭代 for 循环 [英] Iterate over for loop with fixed amount of iterations

查看:33
本文介绍了使用固定数量的迭代迭代 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 ant 脚本中使用了 ant-contrib 库,但我不知道如何制作固定金额使用 foreach 标签的循环?我所说的固定迭代次数并不是指某些硬编码值,而是指从命令行提供的 ant 属性.

I am using ant-contrib library in my ant scripts, but I do not get how can I make a fixed amount of loops using foreach tag? By fixed amount of iterations I do not mean some hardcoded value, but the ant property, supplied from command line.

推荐答案

以下代码创建了一个循环,迭代次数固定为 5 次:

The following code creates a loop with a fixed number of 5 iterations:

<target name="example">
    <foreach param="calleeparam" list="0,1,2,3,4" target="callee"/>
</target>
<target name="callee">
    <echo message="${calleeparam}"/>
</target>

打印

example:
callee:
     [echo] 0
callee:
     [echo] 1
callee:
     [echo] 2
callee:
     [echo] 3
callee:
     [echo] 4

<小时>

编辑

如果您想要可变次数的迭代,那么您可能需要尝试以下方法之一(它们支持的迭代次数和可读性不同).第一种方法可以处理几次迭代.它使用固定列表,使用正则表达式截断该列表以获得固定数量的字符.

If you want a variable number of iterations then you may want to try one of the following approaches (which differ in the number of iterations they support and readability). The first approach can handle a few iterations. It uses a fixed list which gets truncated using a regular expression to get a fixed number of characters.

<target name="example1">
    <property name="n" value="17"/>
    <property name="maxlist" value="00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19"/>
    <propertyregex property="list" input="${maxlist}" regexp="(^.{${n}}.{${n}}.{${n}})" select="\1"/>
    <foreach param="calleeparam" list="${list}" target="callee"/>
</target>

第二种方法可以处理相当多的迭代:它读取一个文本文件并用 X, 替换每个字符以形成一个列表,该列表像前一种方法一样被截断:

The second approach can handle quite a number of iterations: It reads a textfile and replaces every character with X, to form a list which gets truncated like in the previous approach:

<target name="example2">
    <property name="n" value="170"/>
    <loadfile property="chars" srcfile="${ant.file}"/><!-- some large text file -->
    <propertyregex property="maxlist" input="${chars}" regexp="((?:.|[\r\n\t]))" replace="X," global="true"/>
    <propertyregex property="list" input="${maxlist}" regexp="(^.{${n}}.{${n}})" select="\1"/>
    <foreach param="calleeparam" list="${list}" target="callee"/>
</target>

第三种方法使用 JavaScript 为 foreach 准备列表:

The third approach uses JavaScript to prepare the list for foreach:

<target name="example3">
    <property name="n" value="330"/>
    <property name="target" value="callee"/>
    <property name="param" value="calleeparam"/>
    <script language="javascript">
    var list="", n=parseInt(project.getProperty("n"),10);
    for (var i = 0; i &lt; n; i++) list += i + ",";  
    project.setProperty("list", list);
    </script>
    <foreach param="calleeparam" list="${list}" target="callee"/>
</target>

第四种方法不使用foreach,而是使用JavaScript 使用动态创建的antcalls 对目标执行所需数量的调用:

The fourth approach does not use foreach but uses JavaScript to do the desired amount of calls to the target using dynamically created antcalls:

<target name="example4">
    <property name="n" value="3300"/>
    <property name="target" value="callee"/>
    <property name="param" value="calleeparam"/>
    <script language="javascript">
    // does n antcall's with iteration number param
    var n = parseInt(project.getProperty("n"),10);
    for (var i = 0; i &lt; n; i++) {
        var t = project.createTask("antcall");
        t.setTarget(project.getProperty("target"));
        var p = t.createParam();
        p.setName(project.getProperty("param"));
        p.setValue(""+i);
        t.perform();
    }
    </script>
</target>

这篇关于使用固定数量的迭代迭代 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆