in reply to Completing a list/ array?

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

Replies are listed 'Best First'.
Re^2: Completing a list/ array?
by Taylorswift13 (Novice) on Nov 12, 2011 at 15:22 UTC
    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

        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!

      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