in reply to Re^2: Completing a list/ array?
in thread Completing a list/ array?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Completing a list/ array?
by Taylorswift13 (Novice) on Nov 12, 2011 at 17:41 UTC | |
by Not_a_Number (Prior) on Nov 12, 2011 at 18:38 UTC | |
by Taylorswift13 (Novice) on Nov 12, 2011 at 19:09 UTC | |
by aaron_baugher (Curate) on Nov 12, 2011 at 18:36 UTC |