cancel
Showing results for 
Search instead for 
Did you mean: 

UDF Help Please

Former Member
0 Kudos

Hi,

I have a scenario where I have input strings. Within this string there are up to 10 characters (it can be 8, 6, 4, 2, or 0), all of which are 2 character codes. So for example my string may be

abcdefghij

within the above the 2 character codes would be

ab,cd,ef,gh,ij

Now I have a basket of codes that could be present in the above string. Lets say I have 10 codes. So what I need to do is assess the string and see if any of my basket of codes exists within that string. If any one of the 10 2 character codes exist then I want to return confirmation string "Present" the none of the codes are found then I want to return "Not Found".

Could someone help me with this logic for the UDF?

Thank you and points will be rewarded of course.

SA

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Venkata,

I have altered the code a little to suit my needs.

However would it be possible to alter my below code to 1: make it more sophisticated if possible and 2: make it work for a queue of values?

Thanks

SA

if(var1.contains("AB") || var1.contains("BC") || var1.contains("CD")) 
{
return "Present"; 
}
else if(var1.contains("TY") || var1.contains("RE") || var1.contains("ST")) 
{
return "Present 2";
}
else if(var1.contains("11") || var1.contains("33") || var1.contains("22")) 
{
return "Present 3";
}
else
{
return "Not Found";
}

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>make it more sophisticated if possible and

I don't see any syntax or logical flaw in the code. Looks code.

>make it work for a queue of values?

Move the context to the parent node. Right click on the mapping editor and move the context to the parent node.

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

First time in my life I came accross the term "sophisticated codes".

The codes presented here looks quite ok to me.

Could please kindly explain whether you need queue of values as input or as output.

Regards

Anupam

Former Member
0 Kudos

Hi,

> However would it be possible to alter my below code to 1: make it more sophisticated if possible

Code lookds good, you are using single statement in if loop, so braces also not required.

if(var1.contains("AB") || var1.contains("BC") || var1.contains("CD"))

return "Present";

else if(var1.contains("TY") || var1.contains("RE") || var1.contains("ST"))

return "Present 2";

else if(var1.contains("11") || var1.contains("33") || var1.contains("22"))

return "Present 3";

else return "Not Found";

> 2: make it work for a queue of values?

If you change the source field context to its parent node then it works by using below code (Earlier we are using simple UDF, now we have to use queue type UDF and input variable is var1)

String flag="A";

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

{

if(var1{i}.contains("AB") || var1.contains("BC") || var1.contains("CD")) // var1 square bracket open i square bracket close

{

flag="Present";

break;

}

else if(var1.contains("TY") || var1.contains("RE") || var1.contains("ST")) { flag="Present 2"; break; } else if(var1.contains("11") || var1.contains("33") || var1.contains("22"))

{

flag="Present 2";

break;

}

}

if(flag.equals("A"))

result.addValue("Not Found");

else result.addValue(flag);

Regards,

Venkata Ramesh

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

In your code you posted "var1.contains("AB") " if var1 is an input string I could not find any methods called "contains" in java. Thus when you use this code in UDF you will get errors from java "cannot resolve symbol : method contains (java.lang.String)". From all the posts I could make out that you are receiving a queue of values and need to output result based on each value. You have 3 basket of values and the result will depend on whether any of the values in basket are present in string or not. Here is the code you might like to look upon. I chose UDF type context.

 
public   void codes(String[] a,ResultList result,Container container) 
{ 
try 
{ 
int i,j; 
boolean foundCode=false; 
String basketOfCodes1[]={"AB","BC","CD"}; 
String basketOfCodes2[]={"TY","RE","ST"}; 
String basketOfCodes3[]={"11","33","22"}; 
for(i=0;i<a.length;++i) 
{ 
             foundCode=false; 
            for(j=0;foundCode==false && j<basketOfCodes1.length;++j) 
            { 
                         if(a<i>.indexOf(basketOfCodes1[j])>=0) 
                         { 
                                  result.addValue("Present"); 
                                  foundCode=true; 
                         } 
             } 
          for(j=0;foundCode==false && j<basketOfCodes2.length;++j) 
         { 
                       if(a<i>.indexOf(basketOfCodes2[j])>=0) 
                        { 
                               result.addValue("Present 2"); 
                               foundCode=true; 
                        } 
          } 
         for(j=0;foundCode==false && j<basketOfCodes3.length;++j) 
         { 
                        if(a<i>.indexOf(basketOfCodes3[j])>=0) 
                        { 
                                result.addValue("Present 3"); 
                                foundCode=true; 
                        } 
          } 
         if(foundCode==false) 
         { 
                     result.addValue("Not Found"); 
          } 
   } 
} 
   catch(Exception e) 
   { 
           e.printStackTrace(); 
    } 
  return; 
} 

The values in basket of codes may be altered, increased according to your choice.

For the basket of values in the code and array of strings as input these are the output I am receiving

TY---->Present 2

11hjhgk---->Present 3

ABityiufyug---->Present

uyyugyug---->Not Found

Hope this resolves your problem.

Regards

Anupam

Edited by: anupamsap on Feb 7, 2012 10:10 AM

Former Member
0 Kudos

Hi Anupam,

Thank you for the code. Just one thing.... If I wanted to the input to be dynamic. So I wanted the baskets to be inputs in the code i.e. have variables where I would pass in the string of codes, maybe comma separated. How would I handle this?

Thanks

SA

Former Member
0 Kudos

Hi,

if all those values are comming in same field then you can use another variable (var2) and when you are checking the values you need another loop for this.

If those all are comming from different fields then you have to use those many number of input fields and when you are comparing the values you have to use AB[0], BC[0],.... Here AB and BC are input variables.

Regards,

Venkata Ramesh

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

here is the code


public   void codes(String[] a,String[] b,String[] c,String[] d,ResultList result,Container container) 
{ 
try 
{ 
int i,j; 
boolean foundCode=false; 
String basketOfCodes1[]=b[0].split(",");
String basketOfCodes2[]=c[0].split(",");
String basketOfCodes3[]=d[0].split(",");
for(i=0;i<a.length;++i) 
{ 
             foundCode=false; 
            for(j=0;foundCode==false && j<basketOfCodes1.length;++j) 
            { 
                         if(a<i>.indexOf(basketOfCodes1[j])>=0) 
                         { 
                                  result.addValue("Present"); 
                                  foundCode=true; 
                         } 
             } 
          for(j=0;foundCode==false && j<basketOfCodes2.length;++j) 
         { 
                       if(a<i>.indexOf(basketOfCodes2[j])>=0) 
                        { 
                               result.addValue("Present 2"); 
                               foundCode=true; 
                        } 
          } 
         for(j=0;foundCode==false && j<basketOfCodes3.length;++j) 
         { 
                        if(a<i>.indexOf(basketOfCodes3[j])>=0) 
                        { 
                                result.addValue("Present 3"); 
                                foundCode=true; 
                        } 
          } 
         if(foundCode==false) 
         { 
                     result.addValue("Not Found"); 
          } 
   } 
} 
   catch(Exception e) 
   { 
           e.printStackTrace(); 
    } 
  return; 
} 


Now b,c ,d are basket of codes passed as string separeted by commas . Thus input to this UDF will be

String a[]={"TY","11hjhgk","ABityiufyug","uyyugyug"} -


>queue of values.

String b[]={"AB,BC,CD"}----


>only one string neglect curly braces

String c[]={"TY,RE,ST"}----


>only one string neglect curly braces

String d[]={"11,33,22"}----


>only one string neglect curly braces

output received is a queue of values

result[]={"Present 2","Present 3","Present","Not Found"}

Hope this resolves your problem.

One more request, when you post your queries please explain clearly your requirements. To understand your one requirement I had to post 3-4 times. This could have been resolved in one go in only one post. I do not intend to hurt your feelings but you will get your job done quickly if you are clear on what you want.

Regards

Anupam

Answers (3)

Answers (3)

anupam_ghosh2
Active Contributor
0 Kudos

Hi SA,

You have not specified if the basket of codes are being supplied as input to the UDF or not. In case they are not being supplied as input field please follow this UDF


public  String codes(String s,Container container)
	{
		try
		{
			String codes[]={"ab","fg","yy"};
			int i,length=0,j;
			
			if(s!=null && s!="")
			{
				//test if string is null
				length=s.length();
			}
			if(length<=0)
			{
				return "Not Found";
			}
			for(i=0,j=i+2;j<length;i=j+1,j=i+2)
			{
				//check presence of 2 charcater code
				// break original string into 2 chars each.
				String temp=s.substring(i,j);
                                                                                //now compare with basket od values
				for(int k=0;k<codes.length;++k)
				{
					if(temp.equals(codes[k]))
					{
						return "Present";
					}
				}
			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return "Not Found";
	}

Thus if your input is

abcdefghij

the UDF breaks it into pieces as follows ab|cd|ef|gh|ij

the basket is String codes[]={"ab","fg","yy"};

output you get is "Present"

again for the same input string

if the basket of codes is String codes[]={"kk","fg","yy"};

output you get is "Not Found" (as "fg" are not in same segment.).

Please kindly modify the basket of codes in the above UDF as per your requirement. so for ten different codes the string might be

String codes[]={"ab","cd","ef","gh","ij","kl","mn","op","qr","uu"}

In case the basket of codes is an input itself then you can input this as an array of strings to the UDF


public  String codes(String s,String codes[],Container container)
	{
		try
		{
			
			int i,length=0,j;
			
			if(s!=null && s!="")
			{
				//test if string is null
				length=s.length();
			}
			if(length<=0)
			{
				return "Not Found";
			}
			for(i=0,j=i+2;j<length;i=j+1,j=i+2)
			{
				//check presence of 2 charcater code
				// break original string into 2 chars each.
				String temp=s.substring(i,j);
				for(int k=0;k<codes.length;++k)
				{
					if(temp.equals(codes[k]))
					{
						return "Present";
					}
				}
			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return "Not Found";
	}

Regards

Anupam

Edited by: anupamsap on Feb 6, 2012 4:58 PM

Former Member
0 Kudos

Hi,

Thank you for your response.

The only thing is that I can have up to 10 different codes. So does that mean I will need 11 inputs? One for each code and one for the string?

Do I need the inputs? Can I not hard code the codes and then only input the string?

And yes I am only checking to see if a value occurs in the string, I don't care how many times.

Thanks

SA

Former Member
0 Kudos

Hi,

If those values are constant then you can directly hard code those values in UDF itself, at this time you can use simple UDF.

if(var1.contains("AB") || var1.contains("BC") || var1.contains(("CD")) return "Present";

else return "Not Found";

If those values are not constant (varying based on the input) then you can send those values as array (Here you can use queue type UDF)

or you can give multiple input variables for those values (simple UDF).

Regards,

Venkata Ramesh

Edited by: Venkata Ramesh Boppana on Feb 6, 2012 5:00 PM

Former Member
0 Kudos

Hi,

Use the below java code.

Simple UDF with 2 input fields (var1, var2; Var1 is the input string and var2 is the searching value)

if(var1.contains(var2)) return "Present";

else return "Not Found";

If it is array of strings then create queue type udf with 2 input variables (var1, var2; Var1 is the input string and var2 is the searching values) //My Assumption: String comes once and we need to check the multiple values contains in that string or not.

int flag=0;

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

{

if(var1[0].contains(var2<i>))

{

result.addValue("Present");

flag=1;

}

if(flag==1) break;

}

if(flag==0) result.addValue("not found");

Regards,

Venkata Ramesh

Edited by: Venkata Ramesh Boppana on Feb 6, 2012 4:28 PM

Edited by: Venkata Ramesh Boppana on Feb 6, 2012 4:31 PM