#!/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