http://qs1969.pair.com?node_id=90924


in reply to Humanized lists of numbers

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!)

Replies are listed 'Best First'.
Re: Re: Humanized lists of numbers
by CharlesClarkson (Curate) on Jun 24, 2001 at 10:36 UTC
    That's not working for the original list: print join(", ", human(1, 2, 3, 5, 7, 8, 9, 11, 14)), "\n"; prints:1-3, 5-3, 7-9, 11-9, 14-9