Pedlar Decosta Posted May 4, 2019 Share Posted May 4, 2019 (edited) Can someone please tell me why the following code sometimes has a problem- The linked message is received and this code filters out specific words and adds the msg to a list if it isn't one of those words. if((msg !="Name1") ||(msg != "Name2")) { //THEN ADD TO A LIST } However this always works - if(msg != "Name1") { if(msg != "Name2") { //then add to a list } } Obviously I have a simple work around, but I am trying to understand why there is a problem in the first place. Thanks for any help in advance. P.S. Perhaps I should add that what happens is the words in the first example get added to the list even though the filter says not to. Edited May 4, 2019 by Pedlar Decosta Link to comment Share on other sites More sharing options...
Qie Niangao Posted May 4, 2019 Share Posted May 4, 2019 "||" is a logical disjunction, so if either side is true, the conditional is true. And if msg is "Name1" it won't be "Name2" and vice versa, so the disjunction will always be true. If the workaround does what you want, probably you want a conjunction ("&&") instead of a disjunction there, so the test would be for msg being neither of those words. 3 Link to comment Share on other sites More sharing options...
Profaitchikenz Haiku Posted May 4, 2019 Share Posted May 4, 2019 (edited) It might be an advantage to revise the code so it can work with more than two names to be ignored, and which will also simplify the test list known = ["name1", "name2", "name3"]; checkIfKnown( string msg ) { integer found = llListFindList(known, [msg]); // note the list brackets forcing the string to a list type // return value will be 0 or more if the name is found in the list, else -1 if( found == -1 ) { // do stuf // possibly then known += msg; // will add the new name to the list aso it won't be processed again } } Edited May 4, 2019 by Profaitchikenz Haiku 1 Link to comment Share on other sites More sharing options...
Nova Convair Posted May 4, 2019 Share Posted May 4, 2019 (edited) What Qie says! Your non working examble is equivalent to: IF msg!="Name1" OR msg!="Name2" THEN ADD Your working example is equivalent to: IF msg!="Name1" AND msg!="Name2" THEN ADD That's 2 different things. Looks like a mistake on inverting a statement. Example: if (msg=="Name1" || msg=="Name2") Inverting it if ( !(msg=="Name1" || msg=="Name2") ) can be resolved to if (msg!="Name1" && msg!="Name2") Edited May 4, 2019 by Nova Convair 1 Link to comment Share on other sites More sharing options...
animats Posted May 4, 2019 Share Posted May 4, 2019 Right. This is called "DeMorgan's Laws" in Boolean algebra. In LSL form, (!A) || (!B) is equivalent to !(A && B) 1 Link to comment Share on other sites More sharing options...
Pedlar Decosta Posted May 4, 2019 Author Share Posted May 4, 2019 (edited) Ok. That is a little confusing. So even though what I want is to add the msg to the list if it "1 OR the other name", the solution is "1 AND the other name" ? I had assumed the msg would have to be BOTH 1 and 2 for && to work (which is impossible if the names are different). Qie said: "||" is a logical disjunction, so if either side is true, the conditional is true. And if msg is "Name1" it won't be "Name2" and vice versa, so the disjunction will always be true". A: Except the msg could be one of many names. Name1 and Name2 are just names I want to leave off the list. Prof, yes that is a good solution as it is possible I may want to add other names to ignore later. Thank you. Nova - Thanks mate, I didn't know I could use only one set of brackets for it. I assumed they'd have to be separate. (and the && is still playing with my logical thoughts i.e. when AND is actually OR...Even though your explanation is a good one) animats - I had looked at DeMorgans Laws, but because I was thinking OR instead of AND I couldn't resolve it to work how I wanted. This ignorance on my part is one of the many shortfalls (for me at least) of being completely self taught. Edited May 4, 2019 by Pedlar Decosta Link to comment Share on other sites More sharing options...
Rolig Loon Posted May 5, 2019 Share Posted May 5, 2019 I don't know if this helps or not, but think of what the code says in words: The conditional statement if ( (msg != "Name1") || (msg != "Name2") ) {//do something} says,"If it is either FALSE that msg is the same as "Name1" or FALSE that msg is the same as "Name2", do something." In other words, if either one of those statements is FALSE, go ahead and do something. Only one of the two conditions needs to pass the test. So, if msg != Name1, it doesn't matter whether msg == Name2, and vice versa. What you really intended to say was if ( (msg != "Name1") && (msg != "Name2") { // Do something } That is "If it is FALSE that msg is the same as "Name1" and FALSE that msg is the same as "Name2", do something." In other words, if both of those statements are FALSE, do something. Don't do anything unless msg is neither Name1 nor Name2. 2 1 Link to comment Share on other sites More sharing options...
Pedlar Decosta Posted May 5, 2019 Author Share Posted May 5, 2019 Actually Rolig, that does help. It actually makes sense to me now. Everyone was saying virtually the same thing as you, but finally I see the light. Thanks everyone. Thanks Rolig. Really appreciated 1 Link to comment Share on other sites More sharing options...
Pedlar Decosta Posted May 11, 2019 Author Share Posted May 11, 2019 I thought I'd add this in case it helps someone else later on. Something had been bugging me about why I was unable to see the logic behind what was discussed above until several people drilled it into me. And then the reason hit me. If the conditionals had been == (does equal) instead of != (does not equal), the code would have worked perfectly. Which is why I believed the problem to be intermittent. This doesn't work as I intended - if((msg !="Name1") ||(msg != "Name2")) { //THEN ADD TO A LIST } But this DOES - if((msg =="Name1") ||(msg == "Name2")) { //THEN ADD TO A LIST } ...which is obviously where my head was at. And of course that means the solution for the != code - if(msg != "Name1" && msg != "Name2") wont work for the other. if(msg == "Name1" && msg == "Name2") which is an obvious impossibility unless both name1 and name2 are the same. It kind of reminds me of the 'double negative' where " I didn't do nothing" (something you hear all the time) means that you actually did something. Link to comment Share on other sites More sharing options...
Mollymews Posted May 12, 2019 Share Posted May 12, 2019 sometimes it can help when we write out the results. e.g: string msg = "Name1"; if (msg != "Name1" || msg != "Name2") msg != "Name1" is FALSE. msg != "Name2" is TRUE if (FALSE || TRUE) is TRUE. "Name1" is added to list if (FALSE && TRUE) is FALSE. "Name1" is not added to list 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now