ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Reilly <peterrei...@apache.org>
Subject Re: Parsing file. It works but...
Date Mon, 14 Feb 2005 10:57:09 GMT
Disregard,
I have just noticed that you do use a var at
the end of the iteration to "unset" the property!

Peter

Peter Reilly wrote:

> This will not work as each iteration of
> the for loop happens in the same project,
> so source and target will be set only once.
>
> You will need to do something like (note the override attribute
> on the <propertyregex> and the use of <var> instead of <property>):
>
>
>
> <var name="source" value=""/>
> <var name="target" value=""/>
>
> <propertyregex property="source" override="yes"
>                        input="@{line}"
>                        regexp="^(.+)\s(.+)$"
>                        select="\1"
> />
>
> ...
> <if>
>  <equals arg1="${source}" arg2=""/>
>  <then>
>     <var name="source" value="@{line}"/>
>     <var name="target" value="@{line}"/>
>  </then>
> </if>
>
> (Yes it is even ugler!).
>
> You could use a script:
>    <ac:for param="line" list="${lines}">
>      <sequential>
>        <script language="beanshell">
>          items = "@{line}".trim().split(" ");
>          if (items.length > 0) {
>              project.setProperty("source", items[0]);
>              project.setProperty("target", items[items.length-1]);
>          }
>        </script>
>        <echo>source is ${source}, target is ${target}</echo>
>      </sequential>
>    </ac:for>
>
> Peter
>
> Dick, Brian E. wrote:
>
>> ...there has to be a better way to do this.
>>
>> I have a file that contains lines like the following.
>>
>> src1 trg1
>> src2
>> src3 trg3
>>
>> I need to parse the file and send the two values in each line to another
>> program. When there is only one value on the line, I have to send the
>> value twice. Here's the target I wrote to parse the file. I looks pretty
>> ugly. Can anyone simplify this?
>>
>> <target name="test">
>>   <loadfile property="list" srcFile="test_list.txt">
>>      <filterchain>
>>         <tokenfilter delimoutput=","/>
>>      </filterchain>
>>   </loadfile>
>>
>>   <for list="${list}" param="line">
>>      <sequential>
>>         <propertyregex property="source"
>>                        input="@{line}"
>>                        regexp="^(.+)\s(.+)$"
>>                        select="\1"
>>         />
>>
>>         <propertyregex property="target"
>>                        input="@{line}"
>>                        regexp="^(.+)\s(.+)$"
>>                        select="\2"
>>         />
>>
>>         <if>
>>            <not>
>>               <isset property="source"/>
>>            </not>
>>            <then>
>>               <property name="source" value="@{line}"/>
>>               <property name="target" value="@{line}"/>
>>            </then>
>>         </if>
>>
>>         <echo>Process Source=${source} Target=${target}</echo>
>>
>>         <var name="source" unset="true"/>
>>         <var name="target" unset="true"/>
>>      </sequential>
>>   </for>
>> </target>
>>
>> Thanks,
>> BEDick
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>>
>>
>>
>>  
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message