
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>
<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>
<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.
Tweet
Leave a comment