Skip to Content
author's profile photo Former Member
Former Member

UDF Help Please

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 06, 2012 at 04:20 PM

    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";
    }

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • author's profile photo Former Member
    Former Member
    Posted on Feb 06, 2012 at 10:52 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 06, 2012 at 11:27 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 06, 2012 at 11:20 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.