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

Hi Monks, Pls help me locate the reason for the error of the following code,
#! /usr/bin/perl -w open AA, '<',"A.txt" or die "open: $!"; foreach $line(<AA>) { chomp($line); @temp=split(/\./,$line); $new=$temp[0].'.'.$temp[1]; open BB, '<',"B.out" or die "Failed to open B.out: $!"; foreach $line1(<BB>) { @temp1=split(/\s+/,$line1); if($new eq $temp1[0]) { print $temp1[1]."\t"; print $temp1[2]."\n"; } } close (BB); } close(AA);
Small portions of both the files are as follows,
A.txt 1a6m_-.ent.dssp b1b5e_A.ent.dssp b1bx4_A.ent.dssp b1bx7_-.ent.dssp b1bxy_A.ent.dssp
B.out b1a6m_-.ent 79K 83E 127.119 b1a6m_-.ent 80G 84A 136.545 b1b5e_A.ent 123R 127M 132.48 b1bx4_A.ent 61H 65S 145.4
Now, white space b/w column 2 and 3 of B.out is not constant. So while using split in line 11 of my code, I used
@temp1=split(/\s+/,$line1);
and got error Use of uninitialized value in string eq at a.pl line 18, <BB> line 427. but if I use
@temp1=split(/ +/,$line1);
it works pretty fine. What is the reason for this err? Isn't / +/ and /\s+/ refer for same thing? Is there a better way of doing this?

Replies are listed 'Best First'.
Re: "Err:Use of uninitialized value in string eq" while using split
by thundergnat (Deacon) on May 04, 2006 at 14:06 UTC

    You aren't chomping the lines from B.out as you read them so the newline is still on the end. The assertion \s includes newlines, so the split is returning everything after the newline as the last item in the array @temp1. But there isn't anything after the \n, so the last item in the array is null value, hence the uninitialized string warning.

    The fix? chomp the lines as they are read in.

    Update I read the script a little closer. Right effect, wrong reason. Any blank lines in B.out will be completely cleared by /\s+/ so the array will just contain null values. / +/ will leave the \n in the array so the string comparison won't complain.

Re: "Err:Use of uninitialized value in string eq" while using split
by socketdave (Curate) on May 04, 2006 at 14:08 UTC
    What's on line 427 of B.out? Maybe something without whitespace (or the end of the file...)?

    By the way, split magically ignores leading whitespace and splits on subsequent white spaces if you leave off the pattern or give it a literal space...
    @temp1 = split / /, $line1;
      But it is not recognizing leading white spaces just by
      @temp1=split / /,$line1;
      Thats y I hit with
      @temp1=split / +/,$line1;
Re: "Err:Use of uninitialized value in string eq" while using split
by jwkrahn (Abbot) on May 04, 2006 at 23:25 UTC
    Based on the code that you posted, there is no way you could have gotten that warning (unless it is a bug in the version of Perl you are using.) $new is assigned the value $temp[0].'.'.$temp1 so it will never contain the value undef. $temp1[0] is assigned from split(/\s+/,$line1) and $line1 is assigned from a file and the only way that $temp1[0] could contain undef is if $line1 contained '' and it is not possible to have a line like that in a file.