如何根据 Ant 中的限制进行 for 循环

发布于 2024-12-08 04:12:21 字数 229 浏览 0 评论 0原文

在Ant-contrib中,我们可以这样做:

<for list="a,b,c,d" param="instence">

但是如果我没有列表,我只有一个限制,例如limit=4。

有没有办法根据限制进行 for 循环,例如:

<for limit="4" param="index">

In Ant-contrib, we can do something like:

<for list="a,b,c,d" param="instence">

But if I don't have a list, I only have a limit, e.g. limit=4.

Is there a way to do for loop based on limit, like:

<for limit="4" param="index">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜司空 2024-12-15 04:12:22

在antcontrib中你也可以这样做:

<for param="index" end="@{numberOfIterations}" start="1">

但要注意 bug:无法处理
开始和结束是一样的。因此,当“numberOfIterations”等于 1 时,您会收到错误。

我们为此找到了一个解决方法,首先检查 numberOfIterations 的值,并且仅在执行 for 语句时检查 numberOfIterations 的值不为 1。

<if><equals  arg1="1" arg2="@{numberOfIterations}"/>
<then>
   ....
</then>
<else>
   <for param="index" end="@{numberOfIterations}" start="1">
   ....
</else>
</if>

In antcontrib you can also do:

<for param="index" end="@{numberOfIterations}" start="1">

But be aware of a bug: It is unable to handle
begin and end being the same. So when "numberOfIterations" equals 1 you get an error.

We got a workaround for this, checking the value of numberOfIterations first and only when its not 1 doing the for-statement.

<if><equals  arg1="1" arg2="@{numberOfIterations}"/>
<then>
   ....
</then>
<else>
   <for param="index" end="@{numberOfIterations}" start="1">
   ....
</else>
</if>
巷子口的你 2024-12-15 04:12:22

Ant 插件 Flaka 为您的问题提供了一些解决方案。

1)使用 while 循环,

测试属性评估一些 EL 表达式,
这可能是一个简单的计数器,fe

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <!-- some counter -->
 <fl:let>
  countdown = 3
 </fl:let>

 <fl:while test=" countdown >= 0 ">
  <fl:echo>
   #{countdown > 0 ? countdown : 'bang!' }..
  </fl:echo>
  <fl:let>
   countdown = countdown - 1
  </fl:let>
 </fl:while>

</project>

输出:

  [fl:echo] 3..
  [fl:echo] 2..
  [fl:echo] 1..
  [fl:echo] bang!..

或其他一些表达式,fe 检查文件是否存在,意味着循环直到某个文件存在:< br>

 <fl:while test=" !'/home/rosebud/stop'.tofile.isfile ">
  <fl:echo>
   still working..
  </fl:echo>
 </fl:while>

2) 使用带有break 或 continue 的 for 循环:

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <fl:for var="i" in=" list(1,2,3,4,5,6) ">
    <fl:echo>i = #{i}</fl:echo>
    <fl:break test=" i == 3 " />
 </fl:for>

  <fl:for var="i" in=" list(1,2,3,4,5,6) ">
    <fl:continue test=" i > 3 " />
    <fl:echo>i = #{i}</fl:echo>
 </fl:for>

</project>

这会产生相同的结果:

  [fl:echo] i = 1
  [fl:echo] i = 2
  [fl:echo] i = 3

请参阅Flaka 手册

The Ant addon Flaka provides some solutions for your problem.

1) Use a while loop

the test attribute evaluates some EL expression,
which maybe a simple counter, f.e.

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <!-- some counter -->
 <fl:let>
  countdown = 3
 </fl:let>

 <fl:while test=" countdown >= 0 ">
  <fl:echo>
   #{countdown > 0 ? countdown : 'bang!' }..
  </fl:echo>
  <fl:let>
   countdown = countdown - 1
  </fl:let>
 </fl:while>

</project>

output:

  [fl:echo] 3..
  [fl:echo] 2..
  [fl:echo] 1..
  [fl:echo] bang!..

or some other expression, f.e. checking for existence of a file, means looping until some file exists :

 <fl:while test=" !'/home/rosebud/stop'.tofile.isfile ">
  <fl:echo>
   still working..
  </fl:echo>
 </fl:while>

2) Use a for loop with break or continue :

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <fl:for var="i" in=" list(1,2,3,4,5,6) ">
    <fl:echo>i = #{i}</fl:echo>
    <fl:break test=" i == 3 " />
 </fl:for>

  <fl:for var="i" in=" list(1,2,3,4,5,6) ">
    <fl:continue test=" i > 3 " />
    <fl:echo>i = #{i}</fl:echo>
 </fl:for>

</project>

which gives the same result :

  [fl:echo] i = 1
  [fl:echo] i = 2
  [fl:echo] i = 3

See more details in the Flaka manual

再可℃爱ぅ一点好了 2024-12-15 04:12:21
<project name="Attachments" default="print">
    <property name="numAttachments" value="20" />
    <target name="generate">
        <script language="javascript"><![CDATA[
            var list = '1';
            var limit = project.getProperty( "numAttachments" );
            for (var i=2;i<limit;i++)
            { 
                list = list + ',' + i;
            }
            project.setNewProperty("list", list);            
        ]]>
        </script>    
    </target>
    <target name="print" depends="generate">
        <for list="${list}" param="letter">
            <sequential>
                <echo>Letter @{letter}</echo>
            </sequential>
        </for>
    </target>
</project>
<project name="Attachments" default="print">
    <property name="numAttachments" value="20" />
    <target name="generate">
        <script language="javascript"><![CDATA[
            var list = '1';
            var limit = project.getProperty( "numAttachments" );
            for (var i=2;i<limit;i++)
            { 
                list = list + ',' + i;
            }
            project.setNewProperty("list", list);            
        ]]>
        </script>    
    </target>
    <target name="print" depends="generate">
        <for list="${list}" param="letter">
            <sequential>
                <echo>Letter @{letter}</echo>
            </sequential>
        </for>
    </target>
</project>
吃兔兔 2024-12-15 04:12:21

它不完全是你想要的,但是此页面有一个例子。本质上,您将限制作为数字列表传递。它并不优雅,但很有效。

这是另一个想法 - 请参阅用户“cn1h”的回答。这是绕过 ANT 限制的一种很酷的方法 - 嵌入另一种语言的脚本,它可以做你想做的事情。好的!

Its not exactly what you want, but this page has an example. Essentially, you pass in the limit as a list of numbers. Its not elegant, but it works.

Here's another idea - see the answer by user "cn1h". Its a cool way to get around limitations in ANT - embed a script from another language which can do what you want. Nice!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文