sarani has asked for the wisdom of the Perl Monks concerning the following question:
BUT I've been trying to find out why I'm getting warnings of "substr outside of string" from my code for about three hours now, so I'd be really grateful if someone could tell me what I'm doing wrong.
In this section of code, I extract two cartesian coordinates from two different files, save it to two arrays, and compute the distance between them for each pair of coordinates. The format of the two files is such that the coordinate values I need are bang in the middle of the file. Wait, here is the sample data:
HETATM 1562 O HOH 189 48.728 50.544 -16.104
HETATM 1563 O HOH 190 56.288 63.252 16.442
I'm new to perl and I'm used to use C, so I used a nested foreach loop.
I've read the manual, and I've hit search on both google and the Supersearch here. (I haven't waded through the entire mass in that, but I spent about half an hour trying).
The relevant bit of code:
foreach (@hlines) #each line from file1 was saved to this array. { $xh= substr $_,31,8; $yh= substr $_,40,8; $zh= substr $_,48,8; foreach $pline (@plines) #ditto, each line from file2 saved to this array { $x1= substr $pline,31,8; #using $_ here instead of $pline aborts execu +tion due to compile errors. $y1= substr $pline,40,8; $z1= substr $pline,48,8; $r2=((&square($xh-$x1))+(&square($yh-$y1))+(&square($zh-$z1))); $r=sqrt($r2); print "$i. $r2 and square root is $r\n"; } }
The warning shows up for the inner loop, and I'm using strict, subs, and warnings. This is perl v5.8.0 and I'm on a Red Hat 9.0 Linux system.
I don't understand why I'm getting the warning for the inner but not the outer loop.
The manual says that the warning means that I 'looking' outside the string for the substring. All those three substr in the inner loop have as many characters on either side of them as the outer loop substr. 56 char in a line of input, btw.
What am I missing here?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: substr in nested foreach loop
by roboticus (Chancellor) on May 22, 2006 at 12:04 UTC | |
by sarani (Sexton) on May 22, 2006 at 12:15 UTC | |
by sarani (Sexton) on May 22, 2006 at 12:53 UTC | |
by Crackers2 (Parson) on May 22, 2006 at 15:40 UTC | |
by sarani (Sexton) on May 23, 2006 at 03:54 UTC | |
by suaveant (Parson) on May 22, 2006 at 14:39 UTC | |
Re: substr in nested foreach loop
by Jasper (Chaplain) on May 22, 2006 at 17:09 UTC | |
by sarani (Sexton) on May 23, 2006 at 04:02 UTC | |
by Jasper (Chaplain) on May 23, 2006 at 08:50 UTC | |
by sarani (Sexton) on May 23, 2006 at 09:19 UTC | |
Re: substr in nested foreach loop
by Gilimanjaro (Hermit) on May 22, 2006 at 15:19 UTC | |
by sarani (Sexton) on May 23, 2006 at 03:35 UTC | |
by Gilimanjaro (Hermit) on May 23, 2006 at 09:57 UTC | |
Re: substr in nested foreach loop
by rminner (Chaplain) on May 22, 2006 at 15:23 UTC | |
by codeacrobat (Chaplain) on May 22, 2006 at 22:35 UTC | |
by sarani (Sexton) on May 23, 2006 at 04:19 UTC | |
by Anonymous Monk on May 23, 2006 at 05:33 UTC | |
by sarani (Sexton) on May 23, 2006 at 11:12 UTC | |
by sarani (Sexton) on May 23, 2006 at 04:07 UTC |