No letters this week. Instead, I thought I’d introduce some new functions that we recently rolled out. One of the things that lazy productive programmers do is try to find patterns in our code and create new abstractions to hide complexity. I know my readers will scoff at the notion that ShoppingAdvisor contains any complexity, but I have to do SOMETHING to look busy around here.

We analyzed a lot of different business rules and found some common usage patterns—several rules trying to do the same general task—and then created three new functions that we thought would help simplify these rules. Today I’ll go over them and give some simple examples of how to use them.

JOIN
The JOIN function is a lot like CONCATENATE; its purpose is to combine several pieces of text together. What CONCATENATE does not do is separate the pieces of text. Instead, it crams them all together with no breaks. This is fine if you are using CONCATENATE to append query string parameters to a product url, for example:
CONCATENATE($actionurl,"&site=google")

But suppose you want a rule that combines an item’s manufacturer name, mpn, and title, to create a new, improved title. You really need spaces between those pieces. To do that with CONCATENATE, you have to explicitly put a blank space between each piece:
CONCATENATE($manufacturer," ",$mpn," ",$offername)

Not terribly difficult for only three pieces, but you can imagine how it could get cumbersome quickly. Besides that, you can only have 8 inputs to CONCATENATE, and each blank space eats up one of those.

Because this is such a common pattern, we created a new function called JOIN. The example above using JOIN would be:
JOIN(" ",$manufacturer,$mpn,$offername)

You still have put in a blank space, but you only have to do it once. The JOIN puts the space in between all the rest of the inputs for you. Of course, you don’t have to use a blank space; you can put anything there: a comma, the word “plus,” an inventory field, etc. Just remember that whatever is there will be inserted between the other inputs.

ISBLANK
Not to be confused with our old friend IFBLANK. Not the best choice of names, I know, but I’ve used up all my imagination writing these blog posts. IFBLANK outputs text, while the new function ISBLANK outputs a true or false. This is a pattern we actually discovered internally: lots of our filters were checking for things like blank UPCs and blank MPNs by comparing the fields’ lengths to zero:
LENGTH($upc)=0 AND LENGTH($mpn)=0

That’s not terribly onerous, but we figured saving keystrokes adds up, so now you can do this:
ISBLANK($upc) AND ISBLANK($mpn)

That’s a 6% savings right there!

REGEXMATCH
Regular expressions are the Swiss Army Knife of text processing. If the phrase “regular expression” bugs you, just mentally replace it with “pattern.” If you’ve ever done anything in DOS, you’ve probably done something with patterns. Del *.*, for example.

Anyway, REGEXMATCH is another function that outputs true or false. All it does is tell you if text contains a given pattern. For example, suppose you were offering free shipping on certain categories. One way to do this would be to create a lookup list of all the categories and use ISINLIST. However, if you know that the categories are anything that starts with “Gift,” or anything that starts with “Holiday,” and you have a ton of those categories, it might be easier to use REGEXMATCH:
IF(REGEXMATCH($merchantcategory,"^Gift|^Holiday"),"0",$shippingprice)

Let’s break that down. See that “^Gift|^Holiday”? This is the “regular expression” or “pattern” that we’re looking for. The ^ character means “the beginning of the text.” Thus, the pattern “^Gift” means “text that begins with ‘Gift’.” Likewise, “^Holiday” means “text that begins with ‘Holiday’.” The | character just means that either the pattern on the left of the | or the pattern on the right must match. So, “^Gift|^Holiday” means “text that begins with ‘Gift’ or with ‘Holiday’.” And so, REGEXMATCH($merchantcategory,”^Gift|^Holiday”) returns true if an item’s merchantcategory field begins with “Gift” or “Holiday,” and therefore the IF will return a “0″ if it does, and the item’s shippingprice field if not.

There you go, three new functions. That’ll give you all something fun to play with over the long weekend. Enjoy!