I'll make one big assumption: that your lines will be numbered in order. In other words, some may be missing, but line 34 will not come after line 35.

#!/usr/bin/perl use Modern::Perl; my $count = 1; # track the next one to do my $end = 10; # set to 3_000_050 for real run open my $infile, '<', '937753-input.txt' or die $!; while(<$infile>){ if( /(\d+)\s+(\d+)/ ){ # extract two numbers my($k, $v) = ($1, $2); # and put them in named variables if ( $k > $count ) { # see if we skipped any for my $n ($count..($k-1)) { say "$n\t0"; # and ouput them with 0 if we did } } say "$k\t$v"; # then output the current one $count = $k + 1; # and reset the counter } } if ( $count < $end ) { # are any numbers missing at the end? for my $n ($count .. $end) { say "$n\t0"; # output them with zeros } } __END__ # 937753-input.txt 3 5 4 7 6 7 7 0 8 8 # tab-separated output 1 0 2 0 3 5 4 7 5 0 6 7 7 0 8 8 9 0 10 0

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.


In reply to Re^3: Completing a list/ array? by aaron_baugher
in thread Completing a list/ array? by Taylorswift13

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.