Template::Plugin::ListUtil - List::Util functions for TT
[% mylist = [ 1, 2, 3, 4, 4, 3, 2, 5, 2 ] %] [% USE ListUtil %] The largest value in our array is [% ListUtil.largest(mylist) %] [% USE ListUtilVMethods %] The largest value in our array is [% mylist.largest %]
This module provides a selection of handy functions for dealing with lists in the Template Toolkit. Most of the functions are adapted from those provided by or documented in List::Util, though note these have been altered in name and function to work better with the template toolkit.
To access the functions like class methods, simply use the plugin from within a Template Toolkit template:
[% USE ListUtil %]
And then call the method against the ListUtil object.
[% max = ListUtil.largest(mylist) %]
Alternatively you can load the functions as vmethods:
[% USE ListUtilVMethods %] [% max = mylist.largest %]
Using the VMethods plugin as above will cause the vmethods to be in effect for the current template and all templates called from that template. To allow all templates called from any instance of the Template module load the module from Perl with the 'install' parameter.
use Template::Plugin::ListUtilVMethods 'install';
These are the functions that you can use once you've loaded the plugin.
Return the numerically largest value of the list.
Return the largest value of the list, sorted by unicode value
Return the numerically smallest value of the list
Return the smallest value of the list, sorted by unicode value
The sum of adding up all the elements in the list
Returns true if and only if this list contains an even number of items, or the list is empty.
Returns true if and only if this list contains an odd number of items.
The mathematical mean (numerical average) of the list
Mode returns a list of the most frequently occurring elements in a list. For example, for the list:
["buffy", "buffy", "willow", "willow", "buffy" ]
The list
["buffy"]
Would be returned because "Buffy" occurs more times in the list than any other element. However, for some lists have more than one element that could be consider the most frequent:
[ 1, 2, 3, 3, 2, 2, 3, 4, 5, 5 ]
In which case mode returns them all:
mode
[ 2, 3 ]
You can use the virtual method first on the resulting list from mode to pick an arbitrary value, or the mean function (see above) to to take an average of the values.
first
mean
Returns a list containing either the middle element of the list (if the list is odd in length) or the middle two elements of the list (if the list is even in length.) To get a mathematical median you should presort the list (probably with the nsort virtual method) before you pass it to median. Like with mode you can use the virtual method first on the resulting list from median to pick an arbitrary value, or the mean function (see above) to to take an average of the values.
nsort
median
Return a new list made up from randomly shuffled elements of the list passed.
Return a random item from the passed list.
Is at least one item in the list true / false?
Are all items (i.e. every single item) in the list true / false?
Is no element in the list true / false?
Is at least one element in the list false?
How many items are true?
How many items are false?
Written by Mark Fowler <mark@twoshortplanks.com>
Uses the List::Util module, by Graham Barr <gbarr@pobox.com>.
Except as indicated in comments in the code, Copyright Mark Fowler 2003; All Rights Reserved. As indicated by comments in code some code Copyright Graham Barr (1997-2001).
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
None known.
Bugs should be reported to me via the CPAN RT system. http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Plugin-ListUtil.
List::Util (for doing this in Perl)
Template::Plugin::VMethods (details on how the vmethods are installed)
To install Template::Plugin::ListUtil, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Plugin::ListUtil
CPAN shell
perl -MCPAN -e shell install Template::Plugin::ListUtil
For more information on module installation, please visit the detailed CPAN module installation guide.