INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Expressions AND, OR  (Read 4907 times)

HansWei

  • Regular Member
  • Recent member
  • *
  • Posts: 10
Expressions AND, OR
« on: November 26, 2007, 01:24:07 am »

Is it possible to create expressions like:

If ((fieldA=x)AND/OR(fieldB=y), THEN, ELSE) ?

Syntax above shows just principals, not correct MC-syntax, but is this possible in general?

Thx for your help!
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #1 on: November 26, 2007, 07:53:52 am »

I'd actually like to know this too (relating to AND/OR with multiple tag values)... It seems like something MC should be able to do, but the other day I was trying to work it out and couldn't find documentation anywhere in the DevZone or anywhere else.

Expression masters?  What's the syntax?  Or is this one place that MC's expression language is sorely lacking?

EDIT: I see in another thread that this should work with ([Tag1]=Value or [Tag2]=value or [Tag3]=value), however... When I tried it in the format:

([File Type]=mkv or [File Type]=mp4 or [File Type]=mov) it didn't work.

For If support... There is no explicit "else" support, but you can use nested IF statements.  Read the DevZone Expressions document, and then check this thread for an example: http://yabb.jriver.com/interact/index.php?topic=43588.msg297850#msg297850
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #2 on: November 26, 2007, 10:24:51 am »

is this what you guys are after?


Code: [Select]
if(isequal([fieldA],x,8),if(isequal([fieldB],y,8),fieldA = x and fieldB = y,fieldA = x but fieldB does not = y /(pass and//or rule/)),
     if(isequal([fieldB],y,8),fieldA does not = x but fieldB = y /(pass and//or rule/),fieldA does not equal x and fieldB does not equal y /(fail and//or rule/)))

edit...

[file type]=wma or [file type]=ogg or [file type]=mpc worked fine for me here ?

so, you can use a simple 'and/or' search string to obtain a list of files that fit the bill, or you can write an expression that produces differing outputs dependant upon how the and/or rule is satisfied.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #3 on: November 26, 2007, 10:47:29 am »

is this what you guys are after?



Something like that... Not sure exactly what your image is showing, but yes, I'd like to know how to use full Boolean comparison operators including AND, OR, XOR, and NOT.  When I've tried to use it, it didn't seem to work, and there is no documentation that I can find in the Wiki anywhere.

Also, keep in mind that I'm looking for not just [FieldA]=value OR [FieldB]=value, but also [FieldA]=value1 OR [FieldA]=value2 (or AND, XOR, etc).  Keep in mind that some fields (list types) can have more than one value, and some expressions (IF statements) can't easily use [FieldA]=x,y,z (or I couldn't figure it out).

The Expressions documentation could really use some work...  ::)

I think the Parent also wanted to know about IF, THEN, ELSEIF operators, but I suspect my previous posting will cover that question.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #4 on: November 26, 2007, 10:56:38 am »

LOL, this thread is getting hard to follow... see my edited post above. While you were posting, I added the expression below the image.

The image is showing an expression based pane where the expression is asking the and/or questions and providing different outputs per the answers.
If you only want "THEN/ELSE" outputs, then the three "IF"s that pass the rule would return the same value, which would return only 2 entries in the pane, one for pass and one for fail.

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #5 on: November 26, 2007, 12:50:17 pm »

How do you do XOR or NOT?

I'll have to test the OR operator further.  I was hungover and tired last time I did it.   ::)
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #6 on: November 26, 2007, 01:14:39 pm »

'cause I'm a bit ignorant of these things I had to google XOR. I chose this from the results. Is that what you mean?

if yes, I got this from the same expression used above. I just changed the output values:


Though I suspect that I'm missing some crucial piece of understanding here, probably born out of the fact that I have no experience whatsoever with working with SQL databases. I think this alters my perspective a bit because rather than attempt to translate SQL syntax into MC expression syntax, I just try to learn what's possible using the MC tools.

-marko.

EDIT:
I forgot to mention that the reason I wrote the expression the way that I did, using the case-insensitive substring switch, is because I used list type fields to test with, and the expression evaluator works with the raw data, ie. the entire semi-colon delimited string.
If you're working with standard type fields, isequal([fieldA],x,1 would probably be a better choice than isequal([fieldA],x,8

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #7 on: November 26, 2007, 01:29:13 pm »

XOR just means one or the other but not both (exclusive or).
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #8 on: November 26, 2007, 01:34:47 pm »

I think that's that covered then?

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #9 on: November 26, 2007, 02:02:53 pm »

I think that's that covered then?

Well.... I'm not sure at all what your example is doing, or how it relates to what I'm asking...Your's looks a bit more complex than what I'm trying to understand, which is probably why I'm missing something.  I think I just need to play with it...

I'd just like to know the syntax.  What keywords are allowed.... I assume NOT isn't one (which is why you use the "-" sign to exclude results), but is AND?  And I'm not sure from your example which are text and which are keywords.  What is the word "does" in your example? What is the word "not"? Do any of those have special connotations or are they just text?  Sorry.... That example is probably useful to you, but it is way too advanced to get a grip on the basics of the language (with nested IF statements and keywords mixed with text and IsEqual statements and the whole thing)...

So... For example, say I wanted some simple advanced expression columns (not pane) like this:

Code: [Select]
IF ([Keyword]="gangster" OR [Keyword]="criminal"), bad guy, good guy
or

Code: [Select]
IF ([Keyword]="demon" XOR [Keyword]="angel"), supernatural, IF ([Keyword]="demon" AND [Keyword]="angel"), invalid, non-supernatural
or

Code: [Select]
IF ([Keyword]="smart" AND [keyword]="hottie"), compatible, incompatible
Is this the way I'd do it?  What if the there are other keywords... In other words, will [Keyword]=jim match if the keyword field is actually filled with "jim, bob, sue" (three different keywords)?

And, I put quotes around all the above because sometimes I'm searching for "billy-joe bob III" (okay, not that but stuff with spaces).  Does quoted text work?  Or do I need to nest "IsEqual" statements or somethin else?

Like I said.... I think the Expression Language just needs some much better documentation.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #10 on: November 26, 2007, 03:30:25 pm »

glynor, my apologies, I've been far too general and taken far too much for granted...
I have to go to work now, but will fill this in tomorrow.

It's all there in the expression posted above, it just needs the fields changed to those you wish to work with, and the output values changed to those you want to see.
In it's posted form, it will work with keyword fields, yes. will explain more tomorrow.

good night.
-marko.

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9162
Re: Expressions AND, OR
« Reply #11 on: November 27, 2007, 04:08:11 am »

As promised, I get to demonstrate in great detail both my propensity for taking small, very important things for granted, and my very poor tutorial writing skills! Take a couple of Pro-Plus before reading any further...

Probably the single most used expression tool is the if(...) expression. I use them everywhere from the slideshow caption to rename from properties to custom panes, columns or library fields.
We use the other expression tools in conjunction with if(...) to ask questions, to which there can only ever be a yes or no answer, and then output text based on those answers.
Most commonly, if(...) will be used in conjunction with the IsEqual expression, or the IsEmpty expression.
Let's run through some examples using the IsEqual expression.

First we ask the question, then we tell the expression evaluator what to do if the answer is yes, followed by what to do if the answer is no.
In its simplest form, it might look like so: if(isequal([artist],cliff richard,1),this will be rubbish,this might be OK)
This asks: is the artist Cliff Richard? If yes, display "this will be rubbish", and if no, display "this might be OK"
Use that expression to make an expression based column (right click on a column header and choose columns > expression column) to see the expression in action:


Now that's a nice, simple example of an if(...) based expression. It is quite possible to do far more complex things with these because it is perfectly acceptable to ask further if(...) based questions.
Building on our "cliff richard" example: if(isequal([artist],cliff richard,1),if(isequal([name],wired for sound,1),please don't play this,this will be rubbish),this might be OK)
Here, we've asked the question, "is the artist Cliff Richard" and if it is, is the track named "wired for sound"? If the track is called wired for sound, display "please don't play this", if the artist is cliff richard, but the track is not wired for sound, display "this will be rubbish", and if it is not cliff richard, display "this might be OK"


Placement of parenthesis and commas is important. For each opening parenthesis, there must be a closing one to complete the expression. Look at the expression above again.
1st, we ask if the artist is cliff richard, the brown part of the expression deals with what to do if the answer is yes and the expression ends, while the red part deals with what to do if the answer is no, and the expression ends. We could quite feasably ask another question here instead of ending the expression.
When writing complex expressions, I find it helps me to deal with each branch as I come to it, so I deal with each yes answer untill I've finished asking questions, then back track down the string to deal with what to do if the answer is no.

One final point when using the IsEqual expression...
Note that I've used the number one in the artist and name questions above? This tells the evaluator to do a case-insensitive text compare when asking the question. There are 8 different compare modes that can be used and you can read about them on the expressions page. Number eight can be particularly useful as it causes the expression to search for the given string anywhere in the field.

Now, three 'real world' examples which for me, are always the best way to work out what is going on:

Quote
IF ([Keyword]="gangster" OR [Keyword]="criminal"), bad guy, good guy"

The keywords field could possibly contain hundreds of keywords seperated by semi-colons. Here, we use the number 8 to force a case-insensitive substring search of the [keywords] field. First, we ask if the word gangster is there, and if yes, show "bad guy". If no, we then ask if the word criminal is there, and if yes, show "bad guy". For all others, show "good guy". We've asked two questions, and in this example, the two closing parenthesis come at the end of the string, the first of the two closing the second question, and the last one closing the first question.
if(isequal([Keywords],gangster,8),bad guy,if(isequal([keywords],criminal,8),bad guy,good guy))


Quote
IF ([Keyword]="demon" XOR [Keyword]="angel"), supernatural, IF ([Keyword]="demon" AND [Keyword]="angel"), invalid, non-supernatural

Pay attention to the comma that comes after "invalid". The evaluator seperates its commands using commas, so, to tell the evaluator to treat this comma as output text rather than command seperator, it must be escaped using a forward slash.
is "demon" a keyword? if yes, is "angel" also a keyword? if yes, then [keywords] contains "demon" AND "angel" so display "invalid, non-supernatural"
if(isequal([keywords],demon,8),if(isequal([keywords],angel,8),invalid/, non-supernatural,

That takes care of AND. Next, we deal with what to do if "demon" is present, but "angel" is not, so we tell the evaluator to output "supernatural" and close this branch of the expression. This takes care of the first half of the XOR question.
if(isequal([keywords],demon,8),if(isequal([keywords],angel,8),invalid/, non-supernatural,supernatural)

This branch of the expression ends here. There are no more questions to ask and a closing parenthesis is entered. Next, we deal with what to do if "demon" is not in the [keywords] field..
The [keywords] field does not contain "demon", does it contain "angel"? if yes, output "supernatural"
,if(isequal([keywords],angel,8),supernatural,
and that takes care of the second part of the XOR question.

The only thing left now, is to tell the evaluator what to do about all the files that contain neither "demon" nor "angel"
If you want it to return nothing for those files, all that's left to do is close the expression, as shown below. If you would like a label for those other files, place your text between the closing comma and parenthesis. The final expression looks like so:
if(isequal([keywords],demon,8),if(isequal([keywords],angel,8),invalid/, non-supernatural,supernatural),if(isequal([keywords],angel,8),supernatural,)
There is one fly in the ointment here and I don't think it can be worked around. Note that we are doing "substring" searches here. This means that keywords like "demonstrate" or "angelic" would produce matches for "demon" and "angel", possibly skewing the results somewhat, as shown below:



Quote
IF ([Keyword]="smart" AND [keyword]="hottie"), compatible, incompatible

First ask if keywords contains smart, if the answer is yes, ask if it contains hottie, if the answer is yes, output compatible, otherwise, output incompatible. Output incompatible if keywords does not contain smart.
if(isequal([keywords],smart,8),if(isequal([keywords],hottie,8),compatible,incompatible),incompatible)

Quote
What if the there are other keywords... In other words, will [Keyword]=jim match if the keyword field is actually filled with "jim, bob, sue" (three different keywords)?
Expressions work with raw data, so, [keywords]=jim will match from "jim;bob;sue" if you use one of the substring compare types (7 or 8).

Quote
And, I put quotes around all the above because sometimes I'm searching for "billy-joe bob III" (okay, not that but stuff with spaces).  Does quoted text work?
again, expressions work with raw data, so if you use quotes, the evaluator will try to match on them too. spaces are fine. any number of spaces used immediately after a command seperating comma are ignored, everywhere else in the expression, they count as actual data.

I'm not the best communicator in the world, but still, hope it helps a little :)
Fire away if there are any other questions.

-marko.

HansWei

  • Regular Member
  • Recent member
  • *
  • Posts: 10
Re: Expressions AND, OR
« Reply #12 on: November 27, 2007, 04:53:02 am »

woooooooow!
First let me say I am really impressed! marko & glynor, for me you two are one of most respected people in the whole community as i have read so many intelligent things written by you. And BOTH of you hypersonic-superguru's taking care of my question, WOOW - and thank you very much!   :) :) :)  (and it at least proofs that i didn't raise a dummy question)

marko - special thanks for your tutorial as I believe this will help many of us to get a better understanding!
you are right and i can follow your thoughts within the tutorial but at the end of the day this leads e.g. to the following formula (which i am currently using):

clean(If(IsEqual([Media Sub Type],Music Video, 1),\Video\[Media-Source]\[Genre]\[Album Artist]-[Album],If(IsEqual([Media Type],Video, 1),\Video\[Media-Source]\[Genre]\[Album]\,If(IsEqual([Media Type],Video, 1),\Video\[Media-Source]\[Genre]\[Album]\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Media Type],Video, 1),\Video\[Media-Source]\[Genre]\[Album]\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Genre],Classical, 1),\Audio\_Classical\[Album]\,If(IsEqual([Media Type],Video, 1),\Video\[Media-Source]\[Genre]\[Album]\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Genre],Classical, 1),\Audio\_Classical\[Album]\,If(IsEqual([Genre],Soundeffects, 1),\Audio\_Effects\,If(IsEqual([Genre],Classical, 1),\Audio\_Classical\[Album]\,If(IsEqual([Genre],Classical, 1),\Audio\_Classical\[Album]\,\Audio\[Media-Source]\[Album Artist]\[Album]\))))))))))))))))

And this covers only the directory part,... filename is as long as this.

PROS:
Unbelievable, that MC's formula-interpreter is working with this huge stuff. Flawlessly -> Thumbs Up !!!!!

CONS:
Good luck, when you change your mind and it becomes necessary to change something in your formula......  :'( I am currently using a excel-based-formula-generator to handle stuff like this, OK, generator is probably too much, but at least I need some help from excel to prepare these formulas. Right now I am working in including my pictures also - and I bet my formula will grow to double size.....


Conclusion (intermediate, for myself):
MC's power to handle stuff like these formulas is incredible.
With the inclusion of at least if-then-else structure and AND/OR construction it would be even more powerful and formulas would stay more readable and changable.
But still to have the ability to use stuff like this is an enormous Power! Great to have this!  :) :) :) But as glynor mentioned, it needs some explanation, documentation and sample - so special thanks to you marko again for your tutorial!
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #13 on: November 27, 2007, 08:14:54 am »

Thank you so very much, Marko.  This is absolutely fantastic.  I'm actually not done wading through this, but when I have finished I think I might take some time to write it up and throw it into the Wiki (though I still need to finish my View Scheme page).

As I said... I'm not done wading through this, but one thing...

Pay attention to the comma that comes after "invalid". The evaluator seperates its commands using commas, so, to tell the evaluator to treat this comma as output text rather than command seperator, it must be escaped using a forward slash.
is "demon" a keyword? if yes, is "angel" also a keyword? if yes, then [keywords] contains "demon" AND "angel" so display "invalid, non-supernatural"
if(isequal([keywords],demon,8),if(isequal([keywords],angel,8),invalid/, non-supernatural,

I meant to have the comma there, as I do understand the MC IF statement pretty well (and the escape character).  The idea was this (written non-MC methods):

IF keywords = "angel" THEN output "supernatural"
IF keywords = "demon" THEN output "supernatural"
IF keywords = "angel" AND "demon" THEN output "invalid"
IF keywords = none of the above THEN output "non-supernatural"

As I said... Not done wading through, but I got a little confused from your misinterpretation of my meaning, so I thought I'd explain a bit.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Expressions AND, OR
« Reply #14 on: November 27, 2007, 08:28:50 am »

Okay.... I finished reading through and now it is all clear.  So, essentially, boolean logic operators aren't allowed (OR, AND, XOR) except for not (denoted by the preceding "-").  However, using the old-school nested IF statement method you can "fake it".  This is certainly much more unweildy than just using simple logic operators, but at least it is clear how to get what I want.

As far as this:

There is one fly in the ointment here and I don't think it can be worked around. Note that we are doing "substring" searches here. This means that keywords like "demonstrate" or "angelic" would produce matches for "demon" and "angel", possibly skewing the results somewhat, as shown below:



Yeah.... That's really too bad, and is why I didn't immediately go to using the IsEqual substring search operator.

If we had real logic operators, solving the problem would be relatively simple.  You could just do a substring search, and if it succeeded, follow it with another test.  For example (again not written in MC syntax for clarity):

Code: [Select]
IF (IsEqual([keywords],demon,8) THEN
     IF ([keywords]=demon OR (IsEqual,;demon,8) OR (IsEqual,demon;)) THEN output "it's a demon!" ELSE "must be a demonstration!"
ELSE output "doesn't contain demon at all"

But again, it looks like you can accomplish what you want knowing how the syntax works.  It'd just be a heck of a lot easier if the language was a little more advanced!

Thanks!!!
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

Peter Engrav

  • Regular Member
  • Recent member
  • *
  • Posts: 35
  • nothing more to say...
Re: Expressions AND, OR
« Reply #15 on: November 28, 2008, 11:38:29 pm »

I suspect you can do something more graceful than the massive nested IF using string operators.  I haven't tried this yet, but

Foo OR Bar OR Baz could be something like ...

-IsEmpty(if(Foo,A,)if(Bar,B,)if(Baz,C,))
Logged
Pages: [1]   Go Up