9

I have a list of strings (e.g. "piyush,kumar") in an Ant script for which I want to assign piyush to var1 like <var name="var1" value="piyush"/> and kumar to var2 like <var name="var2" value="kumar"/>.

So far, I'm using a buildfile like the following:

<?xml version="1.0"?>
<project name="cutter" default="cutter">
<target name="cutter">
<for list="piyush,kumar" param="letter">
  <sequential>
    <echo>var1 @{letter}</echo>
  </sequential>
</for>
</target>
</project>

I'm not sure how to progress this - any suggestions?

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Piyush
  • 5,145
  • 16
  • 49
  • 71

1 Answers1

12

Here's an example using an ant-contrib variable and the math task:

<var name="index" value="1"/>
<for list="piyush,kumar" param="letter">
  <sequential>
    <property name="var${index}" value="@{letter}" />
    <math result="index" operand1="${index}" operation="+" operand2="1" datatype="int" />
  </sequential>
</for>

<echoproperties prefix="var" />

Output:

[echoproperties] var1=piyush
[echoproperties] var2=kumar

This is all very un-Ant like though - once you've set these what are you going to do with them?

You might consider using an Ant script task instead for this sort of non-declarative processing.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • i m using this in deploymentfile(ant script) for cutting two string in perl file and pushing them in one array then taking that array in deploymentfile(ant script) and cutting those two in Patch name and Deployment machine variable then using these two variable to throughout the ant script so that now no need to declare them in input.properties file. – Piyush Sep 06 '11 at 10:22
  • here we are taking `var` as suffix and we are incrementing the value `1,2...`like. can we change whole thing like first time `patch.name as variable`,second time `deployment.machine as variable`,..... anyway in my script i m changing var1 and var2 to as per my requirement latter in script, but i just want to know can we do like this. – Piyush Sep 06 '11 at 10:30
  • Docs for relevant tasks of 'for', 'foreach', 'PropertySelector': http://ant-contrib.sourceforge.net/tasks/tasks/ – William Denniss Aug 20 '13 at 13:50