Multiple Conditions String

Highlighted
Five Stars

Multiple Conditions String

Hey Guys,

 

I have the following in my tMap:

 

row5.RHTFRM==("502") &&
row5.RGSENTNR==null?
"DE_NOTAP_CD":
"DE_"+ row5.RGSENTART + "_CD"

 

so far so good, no errors.

 

Now I want multiple checks,that means - besides of 502 -  I want to check via or 503, 504 etc.

 

I guess this is quite simple, but I can't get any clue about this.

 

Thanks for your Help!

 

Regards,

 


Accepted Solutions
Sixteen Stars

Re: Multiple Conditions String

You may not have any compilation errors, but that will not work as you expect. If you take the code I gave you and wrap it into a routine (https://community.talend.com/t5/Design-and-Development/Create-a-user-routine-and-call-it-in-a-Job/ta...), you can try the below in your tMap. I am assuming your routine is called "MyRoutines", your method is called "isValueInList", it takes 2 parameters (your input data and a search list) and it outputs a boolean (true or false) if the value is found. This is how the code I gave you yesterday would naturally fit into a routine.

 

row5.RHTFRM==null || 
routines.MyRoutines.findValueInList(row5.RHTFRM,"DE101,DE209,DE401,DE501,DE502,DE503,DE504,DE506,DE507,DE601,DE603,EU.*,RW.*" ?
"DE_NOTAP_CD" : ("DE_"+ row5.RGSENTART + "_CD")

 


All Replies
Forteen Stars TRF
Forteen Stars

Re: Multiple Conditions String

Hi,

 

C

an you be a little more explicit with an example (sample data with expected result)?


TRF
Sixteen Stars

Re: Multiple Conditions String

There are two problems here. The first has been mentioned by @TRF. Essentially are you looking to compare to a fixed, unchanging list of values or are you wanting to compare to potentially dynamic values?

 

The second problem is probably less obvious, but should be looked at as a priority. You are using the == operator for equality of Strings. This is NOT a good idea in Java. To show this, try the following code in a tJava component....

 

String test1 = new String("502");
String test2 = new String("502");
String test3 = "502";

System.out.println("test1 value compared to 502 using ==");
if(test1=="502"){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}

System.out.println("test2 value compared to test1 using ==");
if(test2==test1){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}

System.out.println("test3 value compared to 502 using ==");
if(test3=="502"){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}


System.out.println("test1 value compared to 502 using equals()");
if(test1.equals("502")){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}

System.out.println("test2 value compared to test1 using equals()");
if(test2.equals(test1)){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}

System.out.println("test3 value compared to 502 using equals()");
if(test3.equals("502")){
	System.out.println("PASS");
}else{
	System.out.println("FAIL");
}

This is due to == testing whether it is the same object and equals() tests to see that it is the same value. It is like saying that 2 identical cars are the same car. == asks are they physically the same objects (in the case of the cars that is FALSE). Equals() asks are they the same car (as in make, model, colour, etc)?

 

It is an important distinction to make

Five Stars

Re: Multiple Conditions String

ok agree with you, I will use "equals"

 

The Requirement is:


If

row2.Field10 is

  DE101, DE209, DE401, DE501, DE502, DE503, DE504, DE506, DE507, DE601, DE603  or EU* or RW*

and 

row2.Field11 equals null

   than "DE_NOTAP_CD"

otherwise

  DE_row2.Field12_CD

 

Hope this will make it clearer.

 

Thanks a lot!

Sixteen Stars

Re: Multiple Conditions String

Will....

DE101, DE209, DE401, DE501, DE502, DE503, DE504, DE506, DE507, DE601, DE603  or EU* or RW*

...be a fixed list, or dynamic? Do you want to be able to use wildcards (*)? 

 

Regardless of whether you want the comparable values to be dynamic or not, you can do it with a bit of Java like below. I have made some changes to your compare values. I have ensured that they are regex compliant. SO your wildcard values are now changed. You will see what I've done.

 

If you wrap the below code into a routine with two input parameters (your value and the list to compare to), you can return the String (or wildcard String) that your input value matched to. It may need some tidying up, but this should go some way to helping you solve this...

 

String input = "DE101,DE209,DE401,DE501,DE502,DE503,DE504,DE506,DE507,DE601,DE603,EU.*,RW.*";

String data = "EUdhjhd7";

String[] tokens = input.split(",");

boolean notFound = true;

String tokenMatched = "";

for(int i = 0; i<tokens.length && notFound; i++){
	java.util.regex.Pattern p =  java.util.regex.Pattern.compile(tokens[i]);
	java.util.regex.Matcher m = p.matcher(data);
	
	if(m.matches()){
		notFound = false;
		tokenMatched = tokens[i];
	}

}

System.out.println(tokenMatched);

  

Five Stars

Re: Multiple Conditions String

thanks for your relpy.

 

DE101, DE209, DE401, DE501, DE502, DE503, DE504, DE506, DE507, DE601, DE60

are fixed and

EU* or RW*

are Wildcards.

 

I've tried this in my tMap and get no compilation errors:

 

(row5.RHTFRM.equals("502,DE101,DE209,DE401,DE501,DE502,DE503,DE504,DE506,DE507,DE601,DE603") ||
row5.RGSENTNR==null &&
row5.RHTFRM.equals("EU.*,RW.*"))?

"DE_NOTAP_CD":

("DE_"+ row5.RGSENTART + "_CD")

 

Regards,

 

 

Sixteen Stars

Re: Multiple Conditions String

You may not have any compilation errors, but that will not work as you expect. If you take the code I gave you and wrap it into a routine (https://community.talend.com/t5/Design-and-Development/Create-a-user-routine-and-call-it-in-a-Job/ta...), you can try the below in your tMap. I am assuming your routine is called "MyRoutines", your method is called "isValueInList", it takes 2 parameters (your input data and a search list) and it outputs a boolean (true or false) if the value is found. This is how the code I gave you yesterday would naturally fit into a routine.

 

row5.RHTFRM==null || 
routines.MyRoutines.findValueInList(row5.RHTFRM,"DE101,DE209,DE401,DE501,DE502,DE503,DE504,DE506,DE507,DE601,DE603,EU.*,RW.*" ?
"DE_NOTAP_CD" : ("DE_"+ row5.RGSENTART + "_CD")