[Templates] [PATCH] Make list.slice(1, -1) VMethod work as expected

Paul Evans leonerd-tt at leonerd.org.uk
Fri Mar 14 00:41:09 GMT 2008


The slice() list VMethod suggests you can use negative indices to count
"backwards" from the end of the list. It doesn't quite work as expected
if one index is positive but the other negative. For example, I wanted
all but the first and last elements:

  list.slice(1, -1)

This in fact always returns an empty list.

This can be fixed by:

--- VMethods.pm 2008-03-14 00:37:37.000000000 +0000
+++ VMethods.pm.new     2008-03-14 00:37:04.000000000 +0000
@@ -508,6 +508,8 @@
     my ($list, $from, $to) = @_;
     $from ||= 0;
     $to = $#$list unless defined $to;
+    $from += @$list if $from < 0;
+    $to   += @$list if $to   < 0;
     return [ @$list[$from..$to] ];
 }

-- 
Paul "LeoNerd" Evans

leonerd at leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.template-toolkit.org/pipermail/templates/attachments/20080314/463aa61c/attachment.pgp 


More information about the templates mailing list