splitting an array

Discussion in 'Perl Misc' started by debraj, Sep 6, 2003.

  1. debraj

    debraj Guest

    Hi All ,

    I have one array of numbers say (12 17 18 19 120 121 122 123 124 379
    480 481).
    Now I want to get the starting and ending of any cosecutive numbers
    from this array .
    For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    Note that if a number without any sequence is present it will be
    printed in the format 12--12 .

    Thanx
    debhatta
    debraj, Sep 6, 2003
    #1
    1. Advertising

  2. debraj

    Lao Coon Guest

    (debraj) wrote in
    news::

    > Hi All ,
    >
    > I have one array of numbers say (12 17 18 19 120 121 122 123 124 379
    > 480 481).
    > Now I want to get the starting and ending of any cosecutive numbers
    > from this array .
    > For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    > Note that if a number without any sequence is present it will be
    > printed in the format 12--12 .


    One way:

    my @list = (12,17,18,19,120,121,122,123,123,379,480,481);
    my @res;
    for(my $i = $j = 0; $i < @list; $i++) {
    $j++ if $i && $list[$i] != $list[$i-1]+1;
    push @{$res[$j]}, $list[$i];
    }

    HTH
    Lao
    Lao Coon, Sep 6, 2003
    #2
    1. Advertising

  3. debraj

    Jay Tilton Guest

    (debraj) wrote:

    : I have one array of numbers say (12 17 18 19 120 121 122 123 124 379
    : 480 481).
    : Now I want to get the starting and ending of any cosecutive numbers
    : from this array .
    : For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    : Note that if a number without any sequence is present it will be
    : printed in the format 12--12 .

    my @ary = (12,17,18,19,120,121,122,123,124,379,480,481);
    my $seqs = join ',', map "$_--$_", sort {$a <=> $b} @ary;
    1 while $seqs =~ s/(\d+),(??{$1+1})--(\d+)/$2/;
    print "($seqs)\n";
    Jay Tilton, Sep 6, 2003
    #3
  4. debraj

    Jay Tilton Guest

    (Jay Tilton) wrote:

    : (debraj) wrote:
    :
    : : I have one array of numbers say (12 17 18 19 120 121 122 123 124 379
    : : 480 481).
    : : Now I want to get the starting and ending of any cosecutive numbers
    : : from this array .
    : : For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    : : Note that if a number without any sequence is present it will be
    : : printed in the format 12--12 .
    :
    : my @ary = (12,17,18,19,120,121,122,123,124,379,480,481);
    : my $seqs = join ',', map "$_--$_", sort {$a <=> $b} @ary;
    : 1 while $seqs =~ s/(\d+),(??{$1+1})--(\d+)/$2/;
    : print "($seqs)\n";

    Or, using an actual data structure instead of a string,

    my @ary = (12,17,18,19,120,121,122,123,124,379,480,481);
    my @seqs = map [$_, $_], sort {$a <=> $b} @ary;
    $seqs[$_-1][1] = (splice @seqs, $_, 1)[0]->[1]
    for grep $seqs[$_-1][1] == $seqs[$_][0]-1 => reverse 1..$#seqs;
    print join ',' => map sprintf('%d--%d', @$_), @seqs;

    Kinda fun. I remember seeing variations on this problem solved before on
    clpm, but I couldn't hit on the right Google Groups search terms. Any
    pointers?
    Jay Tilton, Sep 7, 2003
    #4
  5. On Sat, 06 Sep 2003 03:18:25 -0400, debraj wrote:
    > I have one array of numbers say (12 17 18 19 120 121 122 123 124 379 480
    > 481).
    > Now I want to get the starting and ending of any cosecutive numbers from
    > this array .
    > For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    > Note that if a number without any sequence is present it will be
    > printed in the format 12--12 .


    This may not help much, but it works for your case and I learned a lot :)


    _(qw.12 17 18 19 120 121 122 123 124 379 480 481.);
    sub _{
    print($_ = shift);
    while(@_){ ($_[0] == $_ + 1) ? $_ = shift : last; }
    print("--$_\n"), (@_ && _(@_));
    }
    LaDainian Tomlinson, Sep 7, 2003
    #5
  6. debraj

    Philip Lees Guest

    On Sat, 06 Sep 2003 23:05:29 GMT, (Jay Tilton)
    wrote:

    >Kinda fun. I remember seeing variations on this problem solved before on
    >clpm, but I couldn't hit on the right Google Groups search terms. Any
    >pointers?


    Wasn't it one of MJDs quizzes of the week?

    Phil

    --
    Ignore coming events if you wish to send me e-mail
    Philip Lees, Sep 8, 2003
    #6
  7. Also sprach Philip Lees:

    > On Sat, 06 Sep 2003 23:05:29 GMT, (Jay Tilton)
    > wrote:
    >
    >>Kinda fun. I remember seeing variations on this problem solved before on
    >>clpm, but I couldn't hit on the right Google Groups search terms. Any
    >>pointers?

    >
    > Wasn't it one of MJDs quizzes of the week?


    Yes, it was some time ago. The related archive of discussions can
    probably be found somewhere at <http://perl.plover.com/qotw/>.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Sep 8, 2003
    #7
  8. debraj

    Jay Tilton Guest

    "Tassilo v. Parseval" <> wrote:

    : Also sprach Philip Lees:
    :
    : > On Sat, 06 Sep 2003 23:05:29 GMT, (Jay Tilton)
    : > wrote:
    : >
    : >>Kinda fun. I remember seeing variations on this problem solved before on
    : >>clpm, but I couldn't hit on the right Google Groups search terms. Any
    : >>pointers?
    : >
    : > Wasn't it one of MJDs quizzes of the week?
    :
    : Yes, it was some time ago. The related archive of discussions can
    : probably be found somewhere at <http://perl.plover.com/qotw/>.

    Found it.
    http://article.gmane.org/gmane.comp.lang.perl.qotw.quiz-of-the-week/43

    And naturally there's a module squirrelled away on CPAN that does the
    job.
    Jay Tilton, Sep 10, 2003
    #8
  9. debraj

    Anno Siegel Guest

    LaDainian Tomlinson <> wrote in comp.lang.perl.misc:
    > On Sat, 06 Sep 2003 03:18:25 -0400, debraj wrote:
    > > I have one array of numbers say (12 17 18 19 120 121 122 123 124 379 480
    > > 481).
    > > Now I want to get the starting and ending of any cosecutive numbers from
    > > this array .
    > > For eg. result should be (12--12,17--19,120--124,379--379,480--481) .
    > > Note that if a number without any sequence is present it will be
    > > printed in the format 12--12 .

    >
    > This may not help much, but it works for your case and I learned a lot :)
    >
    >
    > _(qw.12 17 18 19 120 121 122 123 124 379 480 481.);
    > sub _{
    > print($_ = shift);
    > while(@_){ ($_[0] == $_ + 1) ? $_ = shift : last; }
    > print("--$_\n"), (@_ && _(@_));
    > }


    Here is a solution based on matching and substitution:

    sub trans {
    my $str = '';
    vec( $str, $_, 8) = ord '1' for @_; # any char except "\0"
    $str =~ s/1+/"$-[ 0]-" . ($+[ 0] - 1)/eg;
    split /\0+/, $str;
    }

    For this the original list doesn't have to be sorted.

    Anno
    Anno Siegel, Sep 12, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    419
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    434
  3. John Dibling
    Replies:
    0
    Views:
    405
    John Dibling
    Jul 19, 2003
  4. Kai Jaensch
    Replies:
    26
    Views:
    709
    Jerry Coffin
    Jan 18, 2004
  5. Kai Jaensch
    Replies:
    3
    Views:
    429
    Kajoka
    Jan 15, 2004
Loading...

Share This Page