On Sat, 02 Feb 2002 01:17:34 +1030, Paul McCann wrote:
>#!/usr/bin/perl -w
>use strict;
>my @list=(15, 18, 30, 31, 42, 45, 46, 47);
>
>my $string=join " ",sort {$a<=>$b} @list;
>$string=~s/(\d+) (?=(\d+))/eval{($2-$1==1)?"$1-":"$1 "}/eg;
>$string=~s/-(?:\d+-)+/-/g;
>print join ", ",split " ",$string;
I don't know why you think you need that "eval". What could ever go
wrong?
And I cleaned up your post-processing. The trick is that if you match
/(PATTERN)+/, the capturing only grabs the last match. The rest is lost.
So "30-31-32" gets replaced with "30-32".
my @list=(15, 18, 30, 31, 32, 42, 45, 46, 47);
my $string = join ", ",sort {$a<=>$b} @list;
$string =~ s/(\d+), (?=(\d+))/($2-$1==1)?"$1-":"$1, "/eg;
$string =~ s/(-\d+)+/$1/g;
print $string;
-->
15, 18, 30-32, 42, 45-47
--
Bart.