ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adam Leggett (UPCO)" <adam.legg...@upco.co.uk>
Subject Re: AW: Parsing comma-separated string and getting handle to each token
Date Mon, 23 Nov 2009 18:05:29 GMT
data.txt :
localVariable1,propVariable11,propVariable12
localVariable2,propVariable21,propVariable22
localVariable3,propVariable31,propVariable32

	
<target name="process-resources">
	<property name="file.path" value="${basedir}/data.txt"/>
	<groovy>
		def f = new File(ant.project.getProperty('file.path'))
		def lines = f.readLines()
		lines.each{ line ->
			def tokens = line.split(',')
			processElement1( tokens[1] )
			processElement2( tokens[2] )
		}
			
		void processElement1(element){
		  println "Processing second element ${element}..."
		}
		
		void processElement2(element){
		  println "Processing third element ${element}..."
		}
	</groovy>	
</target>

output:

process-resources:
   [groovy] Processing second element propVariable11...
   [groovy] Processing third element propVariable12...
   [groovy] Processing second element propVariable21...
   [groovy] Processing third element propVariable22...
   [groovy] Processing second element propVariable31...
   [groovy] Processing third element propVariable32...
BUILD SUCCESSFUL

Thanks

Adam
	
	


On Mon, 2009-11-23 at 06:15 -0800, rohit15 wrote:
> Actually my confusion starts at this point,
> When i use this code , it does print each comma-delimited string ;but i wish
> to do something like this
> 
> <echo>The value of String 1 : ${value1}</echo>
> <echo>The value of String 2 : ${value2}. Now pass this to macro2 </echo>
> <echo>The value of String 3 : ${value3}. Now pass this to macro3</echo>
> 
> To be able to achieve this I would need to distinguish between the String 1
> and String 2. For e.g. value1 or value[1] or any concrete way to
> differentiate between each of the strings
> 
> Thanks,
> Rohit
> 
> 
> 
> Jan.Materne wrote:
> > 
> > <loadfile property="data" srcfile="my.txt"/>
> > <ac:for param="line" list="${data}" delimiter="${line.separator}">
> >   <sequential>
> >     <ac:for param="value" list="@{line}" delimiter=",">
> >       <sequential>
> > 
> > 
> > Jan
> > 
> > 
> >>-----Urspr√ľngliche Nachricht-----
> >>Von: rohit15 [mailto:rohitkulkarni15@gmail.com] 
> >>Gesendet: Montag, 23. November 2009 11:07
> >>An: user@ant.apache.org
> >>Betreff: Parsing comma-separated string and getting handle to 
> >>each token
> >>
> >>
> >>Hi gurus,
> >>
> >>Can you please help me resolve this seemingly simple requirement.
> >>
> >>I have a my.txt file like
> >>localVariable1,propVariable11,propVariable12
> >>localVariable2,propVariable21,propVariable22
> >>localVariable3,propVariable31,propVariable32
> >>
> >>
> >>I need to parse this line by line and then process each line. While
> >>processing each line, I need to execute separate logic on the 
> >>2nd and 3rd
> >>variables of each line.
> >>
> >>I've been able to parse each line using the following code. However, I
> >>haven't succeeded in getting handle to 2nd and 3rd variables 
> >>by reference 
> >>[For e.g. in Java, I'd have iterated using stringtokenizer and 
> >>referred as
> >>stringtkr.token[1], or token[2]  -something on those lines]
> >>
> >>
> >><macrodef name="printline">
> >><attribute name="lineparam"/>
> >><sequential>
> >><echo>Line param :: @{lineparam}</echo>
> >>
> >><for list="@{lineparam}" param="testparam" delimiter=','>
> >><sequential>
> >><property name="@{testparam}" value="This is dummy value for 
> >>@{testparam}"/>
> >><!-- How do I refer specifically to 1st token and then 2nd and 
> >>then 3rd? -->
> >></sequential>
> >></for>
> >></sequential>
> >></macrodef>
> >>
> >>
> >>-- 
> >>View this message in context: 
> >>http://old.nabble.com/Parsing-comma-separated-string-and-gettin
> >>g-handle-to-each-token-tp26474937p26474937.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
> > 
> > 
> > 
> 


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


Mime
View raw message