Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default splitting an array

    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 Guest

  2. Similar Questions and Discussions

    1. Intelligent PDF Splitting
      Does anyone have information about intelligent PDF splitting, using either command line or applescript?
    2. splitting / unpacking line into array
      I have an input file which has many subrecords. The subrecord type is denoted by the first 4 characters of the file. The rest of the line is...
    3. splitting a string
      Hi: I want to split the string 0.0.0.0.1.10.1.30.1.10.1.30.1 into 4 variables: 0.0.0.0, 1, 10.1.30.1 and 10.1.30.1 any suggestions? TIA...
    4. Splitting OR Regex
      On Thu, 30 Oct 2003 23:37:55 -0500, Scott, Joshua wrote: This is a FAQ: perldoc -q delimit -- Tore Aursand <tore@aursand.no>
    5. Splitting a Database
      To All on Access Discussion Forum: I have already split my database. Now I just added a new table to the back-end, but it is not linked to the...
  3. #2

    Default Re: splitting an array

    [email]debhatta@hotmail.com[/email] (debraj) wrote in
    news:f9f243e.0309052318.1696c5bb@posting.google.co m:
    > 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 Guest

  4. #3

    Default Re: splitting an array

    [email]debhatta@hotmail.com[/email] (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 Guest

  5. #4

    Default Re: splitting an array

    [email]tiltonj@erols.com[/email] (Jay Tilton) wrote:

    : [email]debhatta@hotmail.com[/email] (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 Guest

  6. #5

    Default Re: splitting an array

    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 Guest

  7. #6

    Default Re: splitting an array

    On Sat, 06 Sep 2003 23:05:29 GMT, [email]tiltonj@erols.com[/email] (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 Guest

  8. #7

    Default Re: splitting an array

    Also sprach Philip Lees:
    > On Sat, 06 Sep 2003 23:05:29 GMT, [email]tiltonj@erols.com[/email] (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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval Guest

  9. #8

    Default Re: splitting an array

    "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote:

    : Also sprach Philip Lees:
    :
    : > On Sat, 06 Sep 2003 23:05:29 GMT, [email]tiltonj@erols.com[/email] (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.
    [url]http://article.gmane.org/gmane.comp.lang.perl.qotw.quiz-of-the-week/43[/url]

    And naturally there's a module squirrelled away on CPAN that does the
    job.

    Jay Tilton Guest

  10. #9

    Default Re: splitting an array

    LaDainian Tomlinson <go@away.spam> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139