#! /usr/bin/perl -w open AA, '<',"A.txt" or die "open: $!"; foreach $line(Small portions of both the files are as follows,) { chomp($line); @temp=split(/\./,$line); $new=$temp[0].'.'.$temp[1]; open BB, '<',"B.out" or die "Failed to open B.out: $!"; foreach $line1( ) { @temp1=split(/\s+/,$line1); if($new eq $temp1[0]) { print $temp1[1]."\t"; print $temp1[2]."\n"; } } close (BB); } close(AA);
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.4Now, 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,
@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?
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.
@temp1 = split / /, $line1;
@temp1=split / /,$line1;Thats y I hit with
@temp1=split / +/,$line1;
perlmonks.org content © perlmonks.org and cool, jwkrahn, socketdave, thundergnat
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03