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

Hi i have a tsv containing two columns. I have loaded the two columns in seperate rows. I want to subtract the 1st value in second column with the 2nd value in the first column and keep going on till the end of the file. When i try subtracting the 1st element in the second column with the 1st element 1st column it works perfectly fine. When i try to start with the index of 1,0 in array for subtraction, i get error as "use of uninitialized value". please help me i dont know how to figure out the correct assignment.
while (<FILE>) { chomp; ($first,$second)=split ("\t"); my @first_column= split '\n',$first; my @second_column=split '\n',$second; my $a=0; my $b=0; print "$first_column[$a]"; print "\t$second_column[$b]"; my $e=$second_column[$b]-$first_column[$a]; print "\t$e\n"; $a++; $b++; }

Replies are listed 'Best First'.
Re: unable to parse different rows in array
by hdb (Monsignor) on Jul 01, 2013 at 09:36 UTC

    You do not seem to understand the comments you receive in your earlier threads. Re-posting will not get you much in the Monastery.

    Here is a list of your troubles:

    1. $first and $second do not seem to be declared with my.
    2. @firstcolumn and @secondcolumn need to exist during the whole loop and but are being re-instantiated in each iteration. They need to be declared before the loop.
    3. <FILE> reads your file until and including a newline, chomp removes that newline, so $_ contains no newline. And therefore $first and $second do not contain any newlines, splitting on newlines makes no sense.
    4. $a and $b are also re-declared in every iteration of the loop, so their value from previous lines is forgotten every time you read a new line from the file.
    This is not all but should get you started.

Re: unable to parse different rows in array
by marto (Cardinal) on Jul 01, 2013 at 09:34 UTC

    I suggest you read the advice given in your previous thread (unable to parse different rows in array), and all of your other questions. There's a pattern you're either ignoring or not seeing. Read and adviuce you've been given, and how to debug these problems you keep having.

Re: unable to parse different rows in array
by Laurent_R (Canon) on Jul 01, 2013 at 17:47 UTC

    In addition to what has already been said, if you have a tsv i.e., I assume, tab separated values), then you probably want to split on \t rather than \n.