MTSplitLoop

As with other plugins , I've been meaning to write and release this one for quite some time, and I've only now actually done it. So, here is one of the most useful text parsing functions in Perl, wrapped in a Movable Type plugin: MTSplitLoop.

For those unfamiliar with the split function in Perl, here is the quick description taken from the split documentation page at Perldoc.com

Splits a string into a list of strings and returns that list.

In other words, given a string "A,B,C,D,E,F" and the pattern "," to split based on, split returns "A", "B", "C", "D", "E", and "F" so you can process each portion of that string by itself.

Download one of the following files, unpack it, and place splitloop.pl in your plugins/ directory.

The following tags are available:

  • MTSplitLoop: the main container tag that loops through each value in the list returned from the split function. It takes two arguments:
    • value: the string to be split. Can be in embeded MT Tag form (e.g. '[MTTag attr="something"]')
    • glue: the string or regular expression to split on. To split by regular expression, just format glue with slashes ('/') at the beginning and end (e.g. '/\d+/'). Defaults to ','.
  • MTSplitLoopValue: the current value

Usage Example (with Brad's Key Values plugin):

<MTIfKeyExists key="files">
<p>File List</p>
<ul>
<MTSplitLoop value="[MTKeyValue key='files']">
<li><MTSplitLoopValue></li>
</MTSplitLoop>
</ul>
</MTIfKeyExists>

template_tags=MTSplitLoop,MTSplitLoopValue

1 TrackBack

SplitLoop from MT Plugin Directory on June 5, 2003 5:29 PM

Similar to the Perl split command.... Read More

5 Comments

I'd be interested in seeing what you are using this for. :) I've read the description like 5 times now, and still am not *quite* getting it! I think that its like turning something from a list into an array maybe? forgive my perl-ineptitudity ;)

Kristine,

This is a great plug-in. First I stored a list of book ISBNs into an entry using Brad Choate's equally excellent key/values plug-in:

eg:

ukbooks=0716020688,0764552473,1740450329,071814404X,0563534192

Then I used MTSplitLoop to pass each ISBN to MTAmazon with the end result that each entry contains a list of related books. :-)

A very graceful solution. Thanks to all!

All the best,

--
Ian

But can you use this to go from "A;B;C" to "A","B","C"?
From what I can tell, you can only get "A","B","C",.
Note the final comma, which is a significant difference.

it produces lots of errors, and makes MT crash sometimes, here's my apache log:

"my" variable $res masks earlier declaration in same scope at plugins/splitloop.pl line 45.

Out of memory during request for 32 bytes, total sbrk() is 10928128 bytes!

...

:(

This is exactly what I needed, thank you.

No problems with crashes, or slow time.

I would, however, modify your example to include the container tag -- I copy-and-pasted your example and struggled to get it to work, until I realized this slight oversight.

Thanks again.

Leave a comment

Recent Entries

  • Plugin Writing 101

    So, I’m planning on writing up some entries about various aspects of plugin development. While I have a couple topics already in mind, I thought...

  • The Olympics Make Me Want To Compete Again

    Every time the Olympics come around (most the summer ones) I always start to delude myself into thinking I could complete once again in the...

  • Minimalist plugins are fun!

    Last night I whipped up one of the smallest plugins I’ve ever written. It is so small in fact that I was able to stuff...

  • Feedburner Widget on MT News

    Movable Type News A WordPress 2.5 Upgrade Guide: And of course there are lots of third-party plugins for the MT dashboard, to integrate statistics and...

  • AD&D Monster Stats for the Presidential Canditates

    Charles Stross (scifi author, D&D nerd, and former perl columnist) posted Politics as she is Played with 3d6: The recent death of Gary Gygax, who...

Close