#! /root/bin/perl use strict; use warnings; use subs; sub square; sub read; sub dist; my $r; my $r2; my $i=0; my $xh; my $yh; my $zh; my $f1; my $FILE1; my $hline; my @hlines; $f1=&read; my $x1; my $y1; my $z1; my $f2; my $FILE2; my $pline; my @plines; $f2=&read; open (FILE1, "$f1") or die "Argh! File not opened! $!"; open (FILE2, "$f2") or die "Argh! Second file not opened! $!"; while (<FILE1>) { $hline=<FILE1>; push (@hlines,$hline); } + while (<FILE2>) { $pline=<FILE2>; push (@plines,$pline); } close FILE2; close FILE1; print @hlines; print @plines; &dist; exit; sub dist { foreach $hline (@hlines) { $xh= substr $hline,31,8; $yh= substr $hline,40,8; $zh= substr $hline,48,8; foreach $pline (@plines) { print '"',$pline,'": len=',length($pline),"\n"; $x1= substr $pline,31,8; $y1= substr $pline,40,8; $z1= substr $pline,48,8; $i++; $r2=((&square($xh-$x1))+(&square($yh-$y1))+(&square($zh-$z +1))); $r=sqrt($r2); print "$i. $r2 and square root is $r\n"; if(($r<=2.4)&&($r>=3.6)) { print "match!"} } } print $i; return 1; } sub square { my $num; my $sq; $num=shift; $sq = $num * $num; return $sq; } sub read { print "Enter file name:"; $a=<STDIN>; chomp($a); return $a; }

In reply to Re^2: substr in nested foreach loop by sarani
in thread 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.