in reply to number sequence

You want Set::IntSpan. The only way I know this is that newsrc files use that format, and that's what News::Newsrc uses. :)

Update: bobf notes that Set::IntSpan needs a run in increasing order, and that most solutions pretty much do the same thing that I've done below. However, I'm thinking about the next part of the problem, where some part of the program is going to ask if a value is in that list. Making the range is easy with simple Perl. Getting data out of it is a bit more complicated. :)

#!/usr/bin/perl use Set::IntSpan; while( <DATA> ) { chomp; my $set = Set::IntSpan->new( join ",", sort { $a <=> $b } split /,/, $_ ); print "$_ ---> @{ [ $set->elements] }\n"; } __END__ 80 17-199 18,512,21,78 18,5,1790,19-66,212,213
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^2: number sequence
by bobf (Monsignor) on Mar 24, 2007 at 03:57 UTC

    I was going to post the same thing, until I created a test script with the OP's example data. The subsequent error message led me to the docs, where I found this (emphasis mine):

    If a string is given, it is taken to be a run list. A run list specifies a set using a syntax similar to that in newsrc files. A run list is a comma-separated list of runs. Each run specifies a set of consecutive integers.
    Restrictions: The runs in a run list must be disjoint, and must be listed in increasing order.

    Since the OP's example data is not ordered, a solution like the ones provided above is necessary before Set::IntSpan can be used. This, however, makes Set::IntSpan moot for the purpose of the OP. :-)