user765443
user765443

Reputation: 1892

Use of uninitialized value in concatenation (.) or string

I have just beginner in perl and write small piece of code.After Adding \n, I am getting the below error.Please find the table format below.I am trying to fetch ( 2 and 3 column) from temp.txt after index.I am trying to ingnore first two line.

As I mentioned,I got error when I add \n into code

Error : Use of uninitialized value in concatenation (.) or 
        string at temp.pl line 10, <$fh2> line 300.


#! /usr/bin/perl

use strict;
use warnings;

my @data;
open(my $fh2,'<',"temp.txt") or die "Could not open file";
while(my $line =<$fh2>){
     @data =split(/\s+/,$line);
    print  "$data[2]  $data[3]";
    print "\n";
}


      Table format is:
      $DATA1 SOURCE='XtA' VERSION='G-2014.06'
    .TITLE '%curve%'
     index            temp1             temp2          temp3          
                      alter#          
     1               -1.5750000e+00   -3.2053667e+00   -4.0000000e+01  
                       1.0000000e+00(temp4)  

     2               -1.5272727e+00   -2.9323414e+00   -4.0000000e+01  
                       1.0000000e+00  

     3               -1.4795454e+00   -2.6579232e+00   -4.0000000e+01  
                       1.0000000e+00  

    ....................................upto 99 


      temp1  temp2
      <val1>  <val2>

Upvotes: 1

Views: 2293

Answers (2)

Myforwik
Myforwik

Reputation: 3588

After you split do: if (scalar(@data)==4) {

THEN YOUR PRINT CODE

}

Because you don't know if $data[3] or $data[2] exists.

Upvotes: 2

Andy Lester
Andy Lester

Reputation: 93805

You need to give us the full error. You should edit the question and then cut and paste the exact error, including the line number. Then, tell us what line in the file that is.

Chances are that in some of the data in that file there are fewer than 4 fields. Therefore, the split returns 3 or fewer scalars into @data. Then, when you try to refer to $data[3], that's an uninitialized value.

Upvotes: 0

Related Questions