using an array is most likly faster and should require less memory for storage than a hash unless the number of gaps comprise a large part of the data. If that is the case you may wish to consider using a hash:

use warnings; use strict; use constant kMaxGap => 7; my %gappyData; /^(\d+)\s+(\d+)/ and $2 != 0 and $gappyData{$1} = $2 while <DATA>; my $lastx; my $lasty; for (sort {$a <=> $b} keys %gappyData) { next if ! defined $lastx; next if ! defined $lasty; my $gap = $_ - $lastx - 1; next if $gap == 1; next if $gap > kMaxGap; next if $gappyData{$_} != $lasty; $gappyData{$_} = $lasty for $lastx .. $_; } continue { $lastx = $_; $lasty = $gappyData{$_}; } for (1 .. $lastx) { if (defined $gappyData{$_}) { print "$_, $gappyData{$_}\n"; } else { print "$_, -\n"; } } __DATA__ 1 2 2 3 3 3 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 3 12 0 13 0 14 4

Prints:

1, 2 2, 3 3, 3 4, 3 5, 3 6, 3 7, 3 8, 3 9, 3 10, 3 11, 3 12, - 13, - 14, 4

Alternatively you can use a similar technique using missing values represented by undef in an array:

use warnings; use strict; use constant kMaxGap => 7; my @gappyData; /^(\d+)\s+(\d+)/ and $2 != 0 and $gappyData[$1] = $2 while <DATA>; my $lastx; my $lasty; my $currx = 1; for (@gappyData[1..$#gappyData]) { next if ! defined $lastx; next if ! defined $lasty; my $gap = $lastx - $currx - 1; next if $gap == 1; next if $gap > kMaxGap; next if ! defined $gappyData[$currx] or $gappyData[$currx] != $las +ty; $_ = $lasty for @gappyData[$lastx .. $currx]; } continue { $lasty = $_, $lastx = $currx if defined $_; ++$currx; } $currx = 1; for (@gappyData[1..$#gappyData]) { if (defined $_) { print "$currx, $_\n"; } else { print "$currx, -\n"; } ++$currx; }

which generates the same output given the same data.


DWIM is Perl's answer to Gödel

In reply to Re: A Perl-specific solution for a gap bridging problem? by GrandFather
in thread A Perl-specific solution for a gap bridging problem? by a11

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.