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

Hi monkers, Iam back with a struggle trying to subtract elements from two columns and different rows in a array. I have my TSV file as input that has two of my columns. and want to subtract the 0th value of my 2nd column with the 1st value of the 1st column. then again go on subtracting this this way till the end of the column. I tried to put the data into two array and applied the subtraction operator but i am getting error as "use of uninitialized value in subtraction" and there is no output in my file but the size has increased. The code i used is below, and i have no idea where i went wrong! please help.
open (FILE, 'probe_dist.tsv'); open (OFILE,'>probe_sum.tsv'); while (<FILE>) { my @a; my @b; ($a,$b)=split ("\t"); $a=@a; $b=@b; my $c; my $d; my $c=1; my $d=0; my $e=$b[$d]-$a[$c]; $c++; $d++; print OFILE "$e\n"; }
my input file looks like this
4775792 4775851 4775842 4775901 4775852 4775911 4775902 4775961 4775952 4776011 4776002 4776061 4776052 4776111 4776102 4776161 4776212 4776271 4776252 4776311 4776302 4776361 4776352 4776411

Replies are listed 'Best First'.
Re: use of uninitialized value
by Anonymous Monk on Jul 01, 2013 at 06:53 UTC
Re: use of uninitialized value
by farang (Chaplain) on Jul 01, 2013 at 07:26 UTC

    The advice of Anonymous Monk above is all excellent, but this for one thing will not fly.

    ($a,$b)=split ("\t"); $a=@a; $b=@b;
    Whatever you have in $a and $b after the split is immediately turned to 0 by assigning empty arrays to those variables.

Re: use of uninitialized value
by hdb (Monsignor) on Jul 01, 2013 at 09:16 UTC

    You seem to want to populate the arrays @a and @b with the data from your file. But you declare them within the loop over the lines of your file. So for every line you read you create new versions of these arrays and forget all information from previous lines. This is surely not what you want. To make your data persistent across the lines of your file, you need to declare common structures before you enter the loop.

    my @a; my @b; while (<FILE>) { ...