Comparing Two Lists Using Movable Type Template Tags

| No Comments | No TrackBacks |
A recent forum posting asked for an easy way to compare two lists in Movable Type, preferably using arrays.  I think it would be possible to do this via some arrays and/or some clever regular expression use with the 'like' attribute, but in my experience using a hash for this works much faster and doesn't depend on the values in the list behaving nicely in a regular expression context.  Here is an example I cooked up:
The problem
Suppose you want to compare the most recent entry titles on two blogs and display a message if two of them match.  An easy way to do this would be:

<mt:entries blog_ids="5">
<mt:setvarblock name="thetitle"><mt:entrytitle></mt:setvarblock>
<mt:var name="thelist{$thetitle}" value="1">
</mt:entries>

<mt:entries blog_ids="2">
<mt:setvarblock name="thetitle"><mt:entrytitle></mt:setvarblock>
<mt:if name="thelist{$thetitle}">"<mt:entrytitle>" occurs in both blogs!<br></mt:if>
</mt:entries>

Explanation
Basically we use the title of each entry as the keys in an array, with a value of '1' linked to each key.  In the second loop we can then just simply check if a value is present in the array if we use the current entry title as the key.  Since this is a simple lookup operation, we don't need to waste time looping through the entire list.  This helps a lot if there are a lot if elements to compare, believe me!

Comparing the full list
This is not that much more complex:

<mt:entries blog_ids="5">
<mt:setvarblock name="thetitle"><mt:entrytitle></mt:setvarblock>
<mt:var name="thelist{$thetitle}" value="1">
<mt:setvarblock name="lenghtoffirst"><mt:var name=__counter__"></mt:setvarblock>
</mt:entries>

<mt:var name="match" value="1">

<mt:entries blog_ids="2">
<mt:setvarblock name="thetitle"><mt:entrytitle></mt:setvarblock>
<mt:unless name="thelist{$thetitle}"><mt:var name="match" value="0"></mt:unless>
<mt:setvarblock name="lenghtofsecond"><mt:var name=__counter__"></mt:setvarblock>
</mt:entries>

<mt:if name="match">
<mt:if name="lenghtoffirst" eq="$lenghtofsecond">
Match!
</mt:if>
</mt:if>

Explanation
Besides the title of each entry, we now also store the value of the loop variable __counter__ during each iteration through the mt:entries loops.  This means after each loop we end up with the exact number of items found during the loop, neatly stored in a variable we can use later on.

We also use a helper variable called $match.  It is set to "1" before we start our comparison, since two empty lists should be considered identical.  As soon as we find an item in the second list that is not present in the hash generated from the first list we set it to zero, indicating a non-matching item was found during the second loop.

Finally, we look at what we have left after the two loops: if no non-matching items were found, and the lists are both of the same lenght, declare a match.  Else, do nothing.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/51

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be
Powered by Movable Type 5.2.7