[Templates] Sorting one element arrays
Chris Dean
ctdean@babycenter.com
Tue, 01 Aug 2000 16:46:50 -0700
Sorting one element arrays of hashes seems to automagically change the
type of variable from an Array to a Hash. This creates problems when,
for example, sorting before calling FOREACH.
Consider this code:
[% longarray = [ { name => "Austria", count => 7 },
{ name => "China", count => 5 },
{ name => "Brazil", count => 6 },
{ name => "Denmark", count => 7 } ] %]
[% shortarray = [ { name => "England", count => 9 } ] %]
longarray = [% longarray %]
longarray = [% longarray.sort( "name" ) %]
shortarray = [% shortarray %]
shortarray = [% shortarray.sort( "name" ) %]
shortarray turns into a Hash ref after the sort.
The fix seems trivial, but please double check since I'm not exactly
sure what Stash.pm is doing :-)
Regards,
Chris Dean
--- lib/Template/Stash.pm.orig Tue Aug 1 16:42:35 2000
+++ lib/Template/Stash.pm Tue Aug 1 16:43:37 2000
@@ -73,6 +73,7 @@
},
'sort' => sub {
my ($list, $field) = @_;
+ return $list unless $#$list;
return $field # Schwartzian Transform
? map { $_->[0] } # for case insensitivity
sort { $a->[1] cmp $b->[1] }