cancel
Showing results for 
Search instead for 
Did you mean: 

PI Message Mapping Code Convert Array to Comma Separated String

former_member191528
Participant
0 Kudos

Hello

Can anyone please help me with this issue.

We want to convert an array of elements into a comma seperated string. Can you please guide us on this issue.

The structure is as follows

VendorID

Item0

Item1

Item2

Item3

to a string with Item0,Item1,Item2,Item3

Thanks.

Kiran

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

udf as below...

function convertArrayToString( String[] elements  )
{
  StringBuffer s = new StringBuffer();
  for (int i = 0; i < elements.length; i++ )
  {
    s.append ( elements[ i ] );
    if ( i != ( elements.length - 1 ) )   
    {
      s.append ( "," );
    }
  }
  return s.toString();
}

former_member191528
Participant
0 Kudos

Thanks Anand for your quick response. Can you please guide me on how to input an array into a udf. Which execution type one should select when creating the udf

1) Single Values

2) All values of a context

3) All values of a queue

Thanks.

Kiran

Former Member
0 Kudos

Hi Kiran

Try this simple UDF type context

public void getitem(String[] items,ResultList result,Container container){
for(int i=0;i<items.length;i++){
result.addValue(items<i>+",");
}

Replace items with parent node of items and map it to parent node. or keep items your choice

Irrespective of number of nodes under parent node it will add comma to all and return to single field.

Thanks

Gaurav

former_member191528
Participant
0 Kudos

Hello Gaurav,

Thanks for providng a detailed way to resolve the issue.

I just wanted to confirm if I can use

VendorID

1234

45

567

and map it to string element VendorListing so that Vendor Listing has 1234,45,567 at the end of the mapping

Thanks.

Kiran

Former Member
0 Kudos

Hi Kiran

Code i provided can be used like this

assuming structure is

VendorID
   item1
   item2
   item3

then use the code as VendorID -> UDF - target field.

Target will get item1,item2,item3

It will take any number of item fields user VendorID.

Give it a try and let us know if face problem.

Thanks

Gaurav

Former Member
0 Kudos

>

public void getitem(String[] items,ResultList result,Container container){
> for(int i=0;i<items.length;i++){
> result.addValue(items<i>+",");
> }

>

Gaurav,

I doubt this gonna work. With the above code, this will be output i guess.

Input:

Vendor

item1

item2

item3

Output:

item1,

item2,

item3,

You need to use addValue only once.. probably following code will work


public void getitem(String[] items,ResultList result,Container container)
{
StringBuffer b = new StringBuffer();
for(int i=0;i<items.length;i++){
  b.append( items<i> ).append(",");
}
result.addValue( b.toString( ) );

Former Member
0 Kudos

Hey Anand

Its working for me and i checked in my system before providing to Kiran

Well to execute result.add in a loop is doing the same operation as append

Try it once. let me also know the case where it failed so that i can change the code as per your directions.

We can improve this and Kiran get the best out.:)

Thanks

Gaurav

former_member191528
Participant
0 Kudos

Thanks Anand and Gaurav,

I am using the following code which is the combined code provided by both of you.


public void calculate(String[] items, ResultList result, Container container) 
{

StringBuffer b = new StringBuffer();
for(int i=0;i<items.length;i++)

{
  b.append( items<i> );

    if( i != ( items.length - 1 ) ) 
    b.append(",");
}

result.addValue( b.toString( ) );


}

I appreciate all your help with the issue.

Former Member
0 Kudos

Hey Anand

Just Checked again with the code. you are right it will put one extra ",". Thanks for correcting me.

Well My code needs the extra one so i mistook it.

Thanks

Gaurav

former_member191528
Participant
0 Kudos

Thanks Anand and Gaurav for your valuable guidance. This change in mapping helped us workaround without modifying program in the backend BI system. Since we are already in QA phase we will be able to do PI service without modifying existing BI modules.

Best Regards,

Kiran

Answers (0)