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