You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Raagu <rk...@gmail.com> on 2009/08/05 11:45:58 UTC

Reversing a list

Hello People

I have a written a ant script which has a variable say "list" which contains
list of names say name1,name2,ant,forum,nabble     . All r comma separated..

I want to reverse this list such dat , after reversing "list" should give me  
nabble,forum,ant,name2,name1..

And also Is there any option to discard  some of the values from list..??
Please reply 
Thanks in advance
-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24823814.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


Re: AW: AW: Reversing a list

Posted by Eric Fetzer <el...@yahoo.com>.
If you want to reuse the same property, there is a task in antelope called unset.  You can use this task so that you can change the value of a property.  I would create a macrodef if using it a lot:

<taskdef name="unset" classname="ise.antelope.tasks.Unset"/>

<macrodef name="modifyProperty">
  <attributes var.propertyName/>
  <attributes var.propertyValue/>
  <sequential>
    <unset name="@{var.propertyName}" />
    <property name="@{var.propertyName}" value="@{var.propertyValue}" />
  </sequential>
</macrodef>




 



________________________________
From: Raagu <rk...@gmail.com>
To: user@ant.apache.org
Sent: Wednesday, August 5, 2009 11:09:23 PM
Subject: RE: AW: AW: Reversing a list


Hello people 

I have used javascript code written above and i gave new proerty name for
each iteration while calling javascript scriptdef.. It works fine..

Thanks for useful replies :)




Joshua Wunder-2 wrote:
> 
> For these situations I like to use the following:
> 
> http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html
> 
> -----Original Message-----
> From: Raagu [mailto:rknilekani2005@gmail.com] 
> Sent: Wednesday, August 05, 2009 16:36
> To: user@ant.apache.org
> Subject: Re: AW: AW: Reversing a list
> 
> 
> "Try giving the property a new name"
> 
> I didnt get this.. 
> pls can u explain ??
> 
> 
> 
> 
> Jan.Materne wrote:
>> 
>>>How can i access the the newlist here (outside <groovy> task)
>>>I tried            ${list}
>>>But it is giving old values .. which is set initially..
>>>please do reply.
>> 
>> Maybe you are not doing anything wrong.
>> Properties in Ant are immutable. Try giving the property a new name.
>> 
>> Jan
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Reversing-a-list-tp24823814p24827129.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> 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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24840410.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


      

RE: AW: AW: Reversing a list

Posted by Raagu <rk...@gmail.com>.
Hello people 

I have used javascript code written above and i gave new proerty name for
each iteration while calling javascript scriptdef.. It works fine..

Thanks for useful replies :)




Joshua Wunder-2 wrote:
> 
> For these situations I like to use the following:
> 
> http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html
> 
> -----Original Message-----
> From: Raagu [mailto:rknilekani2005@gmail.com] 
> Sent: Wednesday, August 05, 2009 16:36
> To: user@ant.apache.org
> Subject: Re: AW: AW: Reversing a list
> 
> 
> "Try giving the property a new name"
> 
> I didnt get this.. 
> pls can u explain ??
> 
> 
> 
> 
> Jan.Materne wrote:
>> 
>>>How can i access the the newlist here (outside <groovy> task)
>>>I tried             ${list}
>>>But it is giving old values .. which is set initially..
>>>please do reply.
>> 
>> Maybe you are not doing anything wrong.
>> Properties in Ant are immutable. Try giving the property a new name.
>> 
>> Jan
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Reversing-a-list-tp24823814p24827129.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> 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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24840410.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


RE: AW: AW: Reversing a list

Posted by Joshua Wunder <Jo...@cerylion.net>.
For these situations I like to use the following:

http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html

-----Original Message-----
From: Raagu [mailto:rknilekani2005@gmail.com] 
Sent: Wednesday, August 05, 2009 16:36
To: user@ant.apache.org
Subject: Re: AW: AW: Reversing a list


"Try giving the property a new name"

I didnt get this.. 
pls can u explain ??




Jan.Materne wrote:
> 
>>How can i access the the newlist here (outside <groovy> task)
>>I tried             ${list}
>>But it is giving old values .. which is set initially..
>>please do reply.
> 
> Maybe you are not doing anything wrong.
> Properties in Ant are immutable. Try giving the property a new name.
> 
> Jan
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 
> 

-- 
View this message in context:
http://www.nabble.com/Reversing-a-list-tp24823814p24827129.html
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
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


AW: AW: AW: Reversing a list

Posted by Ja...@rzf.fin-nrw.de.
>"Try giving the property a new name"
>
>I didnt get this.. 
>pls can u explain ??

In your script is:

<groovy>
   //Here i am  accessing list as   "project.references.varId.list" . It
will give its values
   // Here i am doing all operation do reverse and filter
   //then i am doing  
   project.references.varId.list = newList
<groovy>

So you are reading project.references.varId.list and storing a new value
into it.
I dont know if Groovy tries to access the value directly or use accessor
methods.
The accessor methods dont allow to change existing values.


Jan

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


Re: AW: AW: Reversing a list

Posted by Raagu <rk...@gmail.com>.
"Try giving the property a new name"

I didnt get this.. 
pls can u explain ??




Jan.Materne wrote:
> 
>>How can i access the the newlist here (outside <groovy> task)
>>I tried             ${list}
>>But it is giving old values .. which is set initially..
>>please do reply.
> 
> Maybe you are not doing anything wrong.
> Properties in Ant are immutable. Try giving the property a new name.
> 
> Jan
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24827129.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


AW: AW: Reversing a list

Posted by Ja...@rzf.fin-nrw.de.
>How can i access the the newlist here (outside <groovy> task)
>I tried             ${list}
>But it is giving old values .. which is set initially..
>please do reply.

Maybe you are not doing anything wrong.
Properties in Ant are immutable. Try giving the property a new name.

Jan

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


RE: AW: Reversing a list

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 
P.S. :

using $attributes.get('in').split(',') because i assumed
your list is given comma separated, f.e. like being set via

<property name="mylist" value="name1,name2,ant,forum,nabble"/>

Regards, Gilbert


-----Original Message-----
From: Rebhan, Gilbert 
Sent: Wednesday, August 05, 2009 3:42 PM
To: 'Ant Users List'
Subject: RE: AW: Reversing a list

 

-----Original Message-----
From: Raagu [mailto:rknilekani2005@gmail.com] 
Sent: Wednesday, August 05, 2009 3:17 PM
To: user@ant.apache.org
Subject: Re: AW: Reversing a list


/*

[..]

How can i access the the newlist here (outside <groovy> task)
I tried             ${list}
But it is giving old values .. which is set initially..
please do reply.

*/


using ruby as scripting language, but it's similar for any scripting
running with BSF, means simply use the ant api.
So for using your newlist outside your script task you have to set
a new property with its value for further processing in your ant script

example with ruby =

<scriptdef name="mylistutil" language="ruby">
  <attribute name="in"/>
  <attribute name="out"/>
  <attribute name="filter"/>
  if $attributes.get('filter').nil?
    $project.setProperty $attributes.get('out') ,
      $attributes.get('in').split(',').reverse.join(',').to_s
  else
    $project.setProperty $attributes.get('out') ,
      $attributes.get('in').split(',').
       reject!{|i| i =~ /#{$attributes.get('filter')}/}.reverse.join(',').to_s
   end
</scriptdef>


and then if filtering (filter attribute takes a regex) wanted just use scriptdef
with filter attribute, f.e. =

<mylistutil in="${mylist}" out="newlist" filter="2$"/>

filters all elements ending with 2 and gives newlist =
nabble,forum,ant,name1

<mylistutil in="${mylist}" out="newlist"/>

runs without filtering and gives newlist =
nabble,forum,ant,name2,name1


*** if you want to overwrite your property mylist, then use mylist in out attribute
and ${mylist} (which is immutable normally) will get overwritten, f.e.

<mylistutil in="${mylist}" out="mylist"/>


Regards, Gilbert


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


Re: AW: Reversing a list

Posted by Raagu <rk...@gmail.com>.
Ok.. But in my script i cant assign newlist values to a property because In
my script newlist values will change dynamically over many iteration and i
need process.. 
Other than assigning to a property Is there any other options ??? 



glenn opdycke-hansen wrote:
> 
> Ant properties are immutable, once the property is given a value that
> value
> cannot be changed. See
> http://ant.apache.org/manual/CoreTasks/property.html
> 
> I believe the suggestion was to define a new property (with a new name).
> The value of the new property can then be assigned.
> 
> 
> -glenn
> 
> 

-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24828299.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


Re: AW: Reversing a list

Posted by glenn opdycke-hansen <gl...@gmail.com>.
Ant properties are immutable, once the property is given a value that value
cannot be changed. See
http://ant.apache.org/manual/CoreTasks/property.html

I believe the suggestion was to define a new property (with a new name).
The value of the new property can then be assigned.


-glenn

RE: AW: Reversing a list

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 

-----Original Message-----
From: Raagu [mailto:rknilekani2005@gmail.com] 
Sent: Wednesday, August 05, 2009 3:17 PM
To: user@ant.apache.org
Subject: Re: AW: Reversing a list


/*

[..]

How can i access the the newlist here (outside <groovy> task)
I tried             ${list}
But it is giving old values .. which is set initially..
please do reply.

*/


using ruby as scripting language, but it's similar for any scripting
running with BSF, means simply use the ant api.
So for using your newlist outside your script task you have to set
a new property with its value for further processing in your ant script

example with ruby =

<scriptdef name="mylistutil" language="ruby">
  <attribute name="in"/>
  <attribute name="out"/>
  <attribute name="filter"/>
  if $attributes.get('filter').nil?
    $project.setProperty $attributes.get('out') ,
      $attributes.get('in').split(',').reverse.join(',').to_s
  else
    $project.setProperty $attributes.get('out') ,
      $attributes.get('in').split(',').
       reject!{|i| i =~ /#{$attributes.get('filter')}/}.reverse.join(',').to_s
   end
</scriptdef>


and then if filtering (filter attribute takes a regex) wanted just use scriptdef
with filter attribute, f.e. =

<mylistutil in="${mylist}" out="newlist" filter="2$"/>

filters all elements ending with 2 and gives newlist =
nabble,forum,ant,name1

<mylistutil in="${mylist}" out="newlist"/>

runs without filtering and gives newlist =
nabble,forum,ant,name2,name1


*** if you want to overwrite your property mylist, then use mylist in out attribute
and ${mylist} (which is immutable normally) will get overwritten, f.e.

<mylistutil in="${mylist}" out="mylist"/>


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


Re: AW: Reversing a list

Posted by Raagu <rk...@gmail.com>.
Hey Thanks for awesome reply..
I have done the same procedure.. I have included groovy jar file n excuted
groovy codes within <groovy></groovy> tags..

Now I am not getting How to pass a variable from <groovy> to the main
script.. i,e How Do i access a variable outside <groovy> tag.. The variable
is set within <groovy> tag..

Here is the sample snippet..
Here list is dynamic and it will keep changing..
that is why i have kept it in <var>

<var id="varID" name="list" value="one,two,three">
<groovy>
   //Here i am  accessing list as   "project.references.varId.list" . It
will give its values
   // Here i am doing all operation do reverse and filter
   //then i am doing   
   project.references.varId.list = newList
<groovy>

After this

How can i access the the newlist here (outside <groovy> task)
I tried             ${list}
But it is giving old values .. which is set initially..
please do reply.




Jan.Materne wrote:
> 
> I dont know any core tasks. Maybe there are external tasks for that.
> Alternatively you could implement your own.
> 
> Jan
> 
> 
> <project>
> 
>     <property name="list"
> value="one,two,three,four,five,six,seven,eight,nine,ten"/>
>     <property name="ignore" value="two,four,six,eight,ten"/>
>     
>     <scriptdef name="revert" language="javascript">
>         <attribute name="list"/>
>         <attribute name="property"/>
>         <![CDATA[
>             listParts = attributes.get("list").split(",");
>             newValue = "";
>             for(i=listParts.length-1; i>=0; i--) {
>                 newValue += "," + listParts[i];
>             }
>             if (newValue.length > 0) {
>                 newValue = newValue.substring(1);
>             }
>             project.setNewProperty(attributes.get("property"), newValue);
>         ]]>
>     </scriptdef>
> 
>     <scriptdef name="filter" language="javascript">
>         <attribute name="list"/>
>         <attribute name="excludes"/>
>         <attribute name="property"/>
>         <![CDATA[
>             listParts = attributes.get("list").split(",");
>             exclParts = attributes.get("excludes").split(",");
>             newValue = "";
>             for(i=0; i<listParts.length; i++) {
>                 add = true;
>                 for(j=0; j<exclParts.length && add; j++) {
>                     add = !listParts[i].equals(exclParts[j]);
>                 }
>                 if (add) {
>                     newValue += "," + listParts[i];
>                 }
>             }
>             if (newValue.length > 0) {
>                 newValue = newValue.substring(1);
>             }
>             project.setNewProperty(attributes.get("property"), newValue);
>         ]]>
>     </scriptdef>
>     
>     
>     <filter property="filtered" list="${list}" excludes="${ignore}" />
>     <revert property="reverted" list="${filtered}"/>
>     <echo>
>        The original list   : ${list}
>        filtered list       : ${filtered}
>        reverted + filtered : ${reverted}
>     </echo>
> 
> </project>
> 
>  
> 
>>-----Ursprüngliche Nachricht-----
>>Von: Raagu [mailto:rknilekani2005@gmail.com] 
>>Gesendet: Mittwoch, 5. August 2009 11:46
>>An: user@ant.apache.org
>>Betreff: Reversing a list
>>
>>
>>Hello People
>>
>>I have a written a ant script which has a variable say "list" 
>>which contains
>>list of names say name1,name2,ant,forum,nabble     . All r 
>>comma separated..
>>
>>I want to reverse this list such dat , after reversing "list" 
>>should give me  
>>nabble,forum,ant,name2,name1..
>>
>>And also Is there any option to discard  some of the values 
>>from list..??
>>Please reply 
>>Thanks in advance
>>-- 
>>View this message in context: 
>>http://www.nabble.com/Reversing-a-list-tp24823814p24823814.html
>>Sent from the Ant - Users mailing list archive at Nabble.com.
>>
>>
>>---------------------------------------------------------------------
>>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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reversing-a-list-tp24823814p24826789.html
Sent from the Ant - Users mailing list archive at Nabble.com.


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


AW: Reversing a list

Posted by Ja...@rzf.fin-nrw.de.
I dont know any core tasks. Maybe there are external tasks for that.
Alternatively you could implement your own.

Jan


<project>

    <property name="list" value="one,two,three,four,five,six,seven,eight,nine,ten"/>
    <property name="ignore" value="two,four,six,eight,ten"/>
    
    <scriptdef name="revert" language="javascript">
        <attribute name="list"/>
        <attribute name="property"/>
        <![CDATA[
            listParts = attributes.get("list").split(",");
            newValue = "";
            for(i=listParts.length-1; i>=0; i--) {
                newValue += "," + listParts[i];
            }
            if (newValue.length > 0) {
                newValue = newValue.substring(1);
            }
            project.setNewProperty(attributes.get("property"), newValue);
        ]]>
    </scriptdef>

    <scriptdef name="filter" language="javascript">
        <attribute name="list"/>
        <attribute name="excludes"/>
        <attribute name="property"/>
        <![CDATA[
            listParts = attributes.get("list").split(",");
            exclParts = attributes.get("excludes").split(",");
            newValue = "";
            for(i=0; i<listParts.length; i++) {
                add = true;
                for(j=0; j<exclParts.length && add; j++) {
                    add = !listParts[i].equals(exclParts[j]);
                }
                if (add) {
                    newValue += "," + listParts[i];
                }
            }
            if (newValue.length > 0) {
                newValue = newValue.substring(1);
            }
            project.setNewProperty(attributes.get("property"), newValue);
        ]]>
    </scriptdef>
    
    
    <filter property="filtered" list="${list}" excludes="${ignore}" />
    <revert property="reverted" list="${filtered}"/>
    <echo>
       The original list   : ${list}
       filtered list       : ${filtered}
       reverted + filtered : ${reverted}
    </echo>

</project>

 

>-----Ursprüngliche Nachricht-----
>Von: Raagu [mailto:rknilekani2005@gmail.com] 
>Gesendet: Mittwoch, 5. August 2009 11:46
>An: user@ant.apache.org
>Betreff: Reversing a list
>
>
>Hello People
>
>I have a written a ant script which has a variable say "list" 
>which contains
>list of names say name1,name2,ant,forum,nabble     . All r 
>comma separated..
>
>I want to reverse this list such dat , after reversing "list" 
>should give me  
>nabble,forum,ant,name2,name1..
>
>And also Is there any option to discard  some of the values 
>from list..??
>Please reply 
>Thanks in advance
>-- 
>View this message in context: 
>http://www.nabble.com/Reversing-a-list-tp24823814p24823814.html
>Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
>---------------------------------------------------------------------
>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


Re: Reversing a list

Posted by David Weintraub <qa...@gmail.com>.
Well, if you don't mind using the AntContrib package, you could do this
using the AntContrib's <for> ,  <if> ,  and the <variable> tasks to build
the string. I haven't tested it, but it would look something like this:

<for
    list="${list}"
    delimiter=","
    param="word">
    <sequential>
        <if>
            <isset property="newlist"/>
            <then>
               <variable name="newlist"
                  value="@{word},${newlist}"/>
            </then>
            <else>
               <variable name="newlist"
                   value="@{word}"/>
            </else>
       </if>
     </sequential>
</for>
<echo>Old List="${list}" New List="${newlist}"</echo>

I tried playing around with regular expressions, but I couldn't get one to
do reversing.

On Wed, Aug 5, 2009 at 5:45 AM, Raagu <rk...@gmail.com> wrote:

>
> Hello People
>
> I have a written a ant script which has a variable say "list" which
> contains
> list of names say name1,name2,ant,forum,nabble     . All r comma
> separated..
>
> I want to reverse this list such dat , after reversing "list" should give
> me
> nabble,forum,ant,name2,name1..
>
> And also Is there any option to discard  some of the values from list..??
> Please reply
> Thanks in advance
> --
> View this message in context:
> http://www.nabble.com/Reversing-a-list-tp24823814p24823814.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


-- 
David Weintraub
qazwart@gmail.com