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 |
In Section
Seekers of Perl Wisdom