From: Mischa POSLAWSKY Date: Sun, 15 Nov 2009 03:43:41 +0000 (+0100) Subject: more legible code to add range ends X-Git-Url: http://git.shiar.nl/perl/list-index.git/commitdiff_plain/e288145c227aef0ecfed7c26e135ca2bbd77c040 more legible code to add range ends --- diff --git a/lib/List/Index.pm b/lib/List/Index.pm index e56d771..719aca7 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -77,12 +77,15 @@ sub ranges { push @links, $link unless $links[-1] eq $link; } + # add range end to each link for my $i (0 .. $#links - 1) { - my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/; - $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 ); - next if $link eq $links[$i] and $i; - $links[$i] .= '-'.$link; + # end at start of next value with the last character decremented + my $next = $links[$i + 1]; + $next =~ s{(.)$}{ $1 le 'a' ? '.' : chr( ord($1) - 1 ) }e; + # amend range if it's ahead + $links[$i] .= '-'.$next unless $next eq $links[$i]; } + # final value takes the rest $links[-1] .= '-'; return \@links;