The code works.

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?


In reply to substr in nested foreach loop by sarani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.