in reply to Re: Completing a list/ array?
in thread Completing a list/ array?

Hey guys

ok sorry i just re-read it and it is very confusing i will give you a better example

This is an example of my Data

Numbers.txt 33 5 34 7 36 7 37 8 38 0 etc

Right now my problem is i have 3 million of these data points and dozens of files. What i am trying to do is make a program so that any numbers missed between 1 and 3000,050 is given the value zero on the right hand side like this

full_numbers.txt 1 0 2 0 3 0 4 0 … 33 5 34 7 36 7 37 8 38 0 … 3000050 0

so all the missing numbers between 1-3000050 are given the value zero

I would like to make the code so that it accepts an input file and the output goes into another file on a tab based spacing format. This is as far as i got

#!/usr/bin/perl $spacer = "\t"; $INPUTFILE = "/Users/ts/desktop/work/numbers.txt"; open(INPUTFILE) or die("File error: input file\n"); open (OUTPUTFILE, "/Users/ts/desktop/work/full_numbers.txt");

OK now i think i need to splice the data since it is in columns then i am unsure.</>

Once again i apologise for any confusion in original question and i hope this clears things help

Thanks in advance

Replies are listed 'Best First'.
Re^3: Completing a list/ array?
by CountZero (Bishop) on Nov 12, 2011 at 15:53 UTC
    Too easy! (To save some space, I limited the example to 50 items only):
    use Modern::Perl; no warnings qw /uninitialized /; my @result; while (<DATA>) { chomp; my ($index, $value) = split /\t/; $result[$index] = $value; } $result[50] = 0 unless $result[50]; while (my ($index, $value) = each @result) { next if $index == 0; say "$index\t", $value+0; } __DATA__ 33 5 34 7 36 7 37 8 38 0
    Output:
    1 0 2 0 3 0 4 0 5 0 6 0 7 0 (...) 31 0 32 0 33 5 34 7 35 0 36 7 37 8 38 0 39 0 (...) 49 0 50 0

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hi thank you for your response. I'm very new to this i tried to adapt it to make an input an output file but it is not working this is what i did, i put in # what i think is happeing and what i don't understand

      #!/usr/bin/perl use Modern::Perl; no warnings qw /uninitialized /; my @result; # assigns a list? $INPUTFILE = "/Users/ts/numbers.txt"; # opens my file open(INPUTFILE) or die("File error: input file\n"); open (OUTPUTFILE, "Users/ts/full_number.txt; #gives output file while (<INPUTFILE>) { chomp; #removes new line my ($index, $value) = split /\t/; # a local list of varibles,not t +o sure what the stuff after spilt is $result[$index] = $value; #? } $result[3000050] = 0 unless $result[3000050]; while (my ($index, $value) = each @result) { next if $index == 0; say "$index\t", $value+0; print (OUTPUTFILE $index\t, $value+0); } close (INPUTFILE); close (OUTPUTFILE)

      am i using the right version of perl for this

      his is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-th +read-multi-2level (with 2 registered patches, see perl -V for more detail)

      once again thank you for your patience!

        Close but no cookie!

        I have edited your code:

        #!/usr/bin/perl use Modern::Perl; #does an implict use strict; use warnings and activa +tes "new" features no warnings qw /uninitialized/; my @result; # declares the array we will use to store the results { # starts a block open my $INPUTFILE, '<', '/Users/ts/numbers.txt' or die "File erro +r: input file: $!"; while (<$INPUTFILE>) { chomp; #removes new line my ($index, $value) = split /\t/; # splits each record at the +tab "\t" -character $result[$index] = $value; # and store the value in the array a +t the right place } } # ends the block, $INPUTFILE goes out of scope and closes the fileha +ndle automatically $result[3000050] = 0 unless $result[3000050]; { # starts a block open my $OUTPUTFILE, '>', '/Users/ts/full_number.txt'or die "File +error: output file: $!"; while (my ($index, $value) = each @result) { next if $index == 0; say $OUTPUTFILE, "$index\t", $value+0; # saves the results in +a >>indexnumber tab value<< format } } # ends the block, $OUTPUTFILE goes out of scope and closes the fileh +andle automatically

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^3: Completing a list/ array?
by aaron_baugher (Curate) on Nov 12, 2011 at 17:18 UTC

    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.

      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;

        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.