http://www.perlmonks.org?node_id=937736

Taylorswift13 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i am a new programmer! I have 2 columns it doesn't start till 5 this is because 1-4 are zero and it doesn't end till 3million but it is meant to end at 3 million and 50 meaning the last 50 values are also zero eg number 5 then tab is assigned value 6, number 6 then tab is assigned value 7, number 7 then tab is assigned value 0, number 3000000 then tab is assigned value 3, How do i fill in the list to make it start at zero and end on 3000050 with every value in between filled in. It would also be good to check i have a value for every number. I am willing to learn so even if you can point me in the right direction that would be useful thanks

Replies are listed 'Best First'.
Re: Completing a list/ array?
by moritz (Cardinal) on Nov 12, 2011 at 12:04 UTC

    For somebody who doesn't know what data you work with, and who doesn't know what your problem is, your question is not understandable.

    Please read How (Not) To Ask A Question, and follow its advise. Post a sample of the input data, the code you've written so far, what your expected outcome is, and where/why you are having trouble producing that output.

Re: Completing a list/ array?
by johngg (Canon) on Nov 12, 2011 at 12:27 UTC

    As moritz points out, your problem description is very confusing. Grabbing at straws, do you mean something like this?

    knoppix@Microknoppix:~$ perl -MData::Dumper -Mstrict -wE ' > my @arr = map { [ $_ => 0 ] } 1 .. 15; > $arr[ $_ - 1 ]->[ 1 ] = $_ for 5 .. 10; > print Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );' @arr = ( [ 1, 0 ], [ 2, 0 ], [ 3, 0 ], [ 4, 0 ], [ 5, 5 ], [ 6, 6 ], [ 7, 7 ], [ 8, 8 ], [ 9, 9 ], [ 10, 10 ], [ 11, 0 ], [ 12, 0 ], [ 13, 0 ], [ 14, 0 ], [ 15, 0 ] ); knoppix@Microknoppix:~$

    Note that array subscripts are zero-based by default in Perl (and it is a default that you *really* don't want to tinker with).

    I hope this is something along the lines of what you are after. If not, have another go at describing your problem more clearly, perhaps with a cut-down data set and the result you are hoping for.

    Cheers,

    JohnGG

      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

        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

        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.

Re: Completing a list/ array?
by Anonymous Monk on Nov 12, 2011 at 11:14 UTC