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:
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))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:
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)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
.
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.