Another solution that works with letter ranges as well, without using any regexes:
#!/usr/bin/perl -w
use strict;
sub human
{
my (@r, $g, $s, $l);
while (@_) {
$l = $g = $s = shift;
$l = shift while (++$g eq ($_[0] || ''));
push(@r, $s eq $l ? $s : "$s-$l");
}
@r;
}
print join(", ", human(1..12, 14..21, 'aa'..'af', 'zz'..'aaf')), "\n";
print join(", ", human(1, 2, 3, 5, 7, 8, 9, 11, 14)), "\n";
Output is:
1-12, 14-21, aa-af, zz-aaf
1-3, 5, 7-9, 11, 14
update: made work with solo items (thanks CharlesClarkson!)