Quantcast

Nested List: programmatically add a list to another list

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Nested List: programmatically add a list to another list

Deininger, Andreas
Hi all,

I'm new to Groovy and ran into the following issue:

<example>
def myList = [ "A", ["B", "C", "D"], "E"];

def flatList = [ "A" ]
flatList += ["B", "C", "D"]
flatList += "E";

myList.each {
        print "$it ";
}
println ""
flatList.each {
    print "$it ";
}
</example>

This gives:
A ["B", "C", "D"] E  
A B C D E

In the second case, I want to get A ["B", "C", "D"] E as result also.
Is there any way to programmatically add a list to another list, producing a nested list?
While using flatList += ["B", "C", "D"], it obvioulsy flattens the list, which is not what I want.

Thanks in advance

Regards
Andreas

PS: I'm using Groovy Version 1.6.3 with JVM 1.6.0_05 on Windows
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Nested List: programmatically add a list to another list

Fred Carter
List l = ["a"]
l << ["b", "c", "d"]
l << "e"
l.each { print "${it} " }
println "done"
produces
    a ["b", "c", "d"] e done
in groovy console

Thus quoth Deininger, Andreas (~ 6/3/2009 8:45 AM ~)...
Hi all,

I'm new to Groovy and ran into the following issue:

<example>
def myList = [ "A", ["B", "C", "D"], "E"];

def flatList = [ "A" ]
flatList += ["B", "C", "D"]
flatList += "E";

myList.each {
	print "$it ";
}
println ""
flatList.each {
    print "$it ";
}
</example>

This gives:
A ["B", "C", "D"] E  
A B C D E

In the second case, I want to get A ["B", "C", "D"] E as result also.
Is there any way to programmatically add a list to another list, producing a nested list?
While using flatList += ["B", "C", "D"], it obvioulsy flattens the list, which is not what I want.

Thanks in advance

Regards
Andreas

PS: I'm using Groovy Version 1.6.3 with JVM 1.6.0_05 on Windows
  


-- 
Fred Carter / AmberPoint, Inc.

[hidden email]
tel:+1.510.433.6525 fax:+1.510.663.6301
Loading...