X-Git-Url: http://git.shiar.net/perl/list-index.git/blobdiff_plain/12c2437bb737f40da7d7514fc31c641ef0110cf3..e288145c227aef0ecfed7c26e135ca2bbd77c040:/lib/List/Index.pm diff --git a/lib/List/Index.pm b/lib/List/Index.pm index f703d97..719aca7 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -74,17 +74,18 @@ sub ranges { $lookahead = $context - $penalty; } - push @links, $link; + push @links, $link unless $links[-1] eq $link; } - use List::MoreUtils 'uniq'; - @links = uniq @links; + # 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;