迭代循环与迭代固定金额 [英] Iterate over for loop with fixed amount of iterations

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

问题描述

我使用蚂蚁的contrib库在我的Ant脚本,但我不明白我怎么能做出一个固定数额
循环的使用的foreach的标签?
通过反复的固定金额我不是说一些硬codeD值,但蚂蚁财产,在命令行提供的。

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.

推荐答案

以下code创建一个具有固定数量的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


修改

如果你想有一个可变次数的迭代,那么你可能想尝试以下方法(在迭代次数不同,他们支持和可读性)之一。第一种方法可以处理几个迭代。它使用了被使用的是常规的前pression获得字符的固定数目截断固定列表

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,来形成被截断像$ P列表$ pvious方式:

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来prepare的名单的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做呼叫所需量使用目标动态创建 antcall 取值:

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>

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

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