cancel
Showing results for 
Search instead for 
Did you mean: 

How to Concatenate all values of the same context

Former Member
0 Kudos

Hello Friends,

I want to concatenate all value of the same context separated by comma and wants a result as an string.

Example.

values in the same context are like this

1

2

3

4

5

The Result should be like this

1,2,3,4,5

Please suggest the correct UDF code for this.

Regards,

Sarvesh

Accepted Solutions (0)

Answers (2)

Answers (2)

GabrielSagaya
Active Contributor
0 Kudos

function myudf(String a[[]], ResultList result,Container container)

{

String target="";

for(int i=0;i<a.length-1;i++)

target=targeta[<i>]"," ;

target=target+a[<i>];

result.addValue(target);

}

/people/harrison.holland5/blog/2006/12/08/mapping-context-changes-in-xi

/people/jeyakumar.muthu2/blog/2005/12/19/data-mining-using-apriori-algorithm-in-xi-150-part-ii

Edited by: Gabriel Sagaya Selvam on Jun 25, 2008 5:09 PM

Former Member
0 Kudos

Hello Gabriel,

you code helpd me lot.

The exact code should be like this.

> function myudf(String a[[]], ResultList result,Container container)

> {

> String target="";

> for(int i = 0;i<a.length;i++)

> target = targeta[<i>]"," ;

> }

> result.addValue(target);

>

Now only the thing I am getting comma at the end of my string, so m working on that.

If you have some suggestion for that you are most welcome.

Regards,

Sarvesh

Former Member
0 Kudos

You can modify ur code as

String target="";

for(int i = 0;i<a.length;i++){

target = target+a<i>;

if ( ! i == a.length-1){ // if i not equal to a.length -1

target = target+"," ;

}

} // end for

Edited by: Mugdha Kulkarni on Jun 25, 2008 3:25 PM

GabrielSagaya
Active Contributor
0 Kudos

in order to avoid the comma at the end

i have modified the code as to have array.length =array.length-1

function myudf(String a[[]], ResultList result,Container container)

{

String target="";

for(int i = 0;i<a.length-1;i++)

target = targeta[<i>]"," ;

target=target+a[<i>];

result.addValue(target);

}

Former Member
0 Kudos

Hi Mugdha,

It works.

Gabriel,

from you solution data is not coming correct.

Anyway now I have got the correct answer.

Thanks a lot to all of you.

Regards,

Sarvesh

Former Member
0 Kudos

Any one... plz give me your valuable suggstions..

Former Member
0 Kudos

Hi Sarvesh,

U can do it with UDF.

Write for loop and inside for loop use the java concat function.

refer the below example

1) "cares".concat("s") returns "caress"

2) "to".concat("get").concat("her") returns "together"

where S replace by a[i[ variable inside the for loop. u also need one variable which stores the previous concat value.

chirag