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

    Hi yes i think your assumption is correct

    I attempted your program but get an error message

     Can't locate Modern/Perl.pm in @INC (@INC contains: /Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.3 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12 .) at ./aari.txt line 2.

    i installed the latest version from the website am i meant to install something different?

    This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-t +hread-multi-2level (with 2 registered patches, see perl -V for more detail) Copyright 1987-2010, Larry Wall

    once again sorry about my dumbnes

    EDIT aha i have installed modern perl

      OK, now that's fixed, try this:

      use Modern::Perl; use autodie; my $infile = '/Users/ts/desktop/work/numbers.txt'; # Hmm... my $outfile = 'Users/ts/full_number.txt'; # leading '/' or n +ot? open my $in, '<', $infile; my @result; while ( <$in> ) { my ( $index, $value ) = split; $result[$index] = $value; } close $in; open my $out, '>', $outfile; say $out "$_\t", $result[$_] || 0 for 1 .. 3_000_050; close $out;

        Hi i tried doing this but i can't seem to open the output file to write in it i checked and it is definatly the right path the same happened with the previous attempt where the results just came up on terminal and not the outputfile i tried ending with a / but no difference

        Can't open 'Users/ts/full_number.txt/' for writing: 'No such file or directory' at ./program13.txt line 18

        EDIT OH i understand now leading /!! yes that fixed it thank you very much

      Sorry, I should have used the built-in stuff. Modern::Perl basically saves a few lines of typing by activating some things that are good practices for every program.

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