Hello Monks, First of all I am sorry for my bad coding practice. I am basically not a programmer but keen to learn Perl. I am trying to write a script which should carryout the following task. I have two files first.txt and second.txt. The first.txt contains, 10, 20, 30, 40. The second.txt contains 1, 2, 3, 4. What I looking is comparing the values of two files and finding difference. I have to compare in the following way.
10 to 1, 20 to 2, 30 to 3 10 to 2, 20, to 3, 30 to 4 20 to 1, 30 to 3, 40 to 4 20 to 2, 30 to 3, 40 to 4
Always, the matching is done with set of three. I am really getting confused with the loops as I never tried programming before. Coming to hash, I require that for further use. If anyone can help me out, it would be appreciated. I am pasting my code below (I know it looks silly but I am learning to do well by using this site) Thank you in advance.

#!/usr/bin/perl use warnings; use strict; my $filename = "first.txt"; my (@queryx, @queryy, @basex, @basey, %hash, $i, $j, $k, $ss); open (FILE, $filename) or die "can't open $filename: $!\n"; while(<FILE>) { if ($_ =~ /(\S+)\s+(\S+)/) { $hash{$1} = $2; push(@queryx,$1); push (@queryy, $2); } } close(FILE); my $filename1 = "second.txt"; open (FILE1, $filename1) or die "can't open $filename: $!\n"; while(<FILE1>) { if ($_ =~ /(\S+)\s+(\S+)/) { $hash{$1} = $2; push(@basex,$1); push (@basey, $2); } } close(FILE1); $ss = @basex; print $ss, "\n"; print "Enter the Threshold Value: \n"; $j= <STDIN>; open(FILEHANDLE, ">result.cmp") or die "cannot open file for reading: +$!";#Creates an output file $k = 0; print $k, "\n"; my $a1 = $queryx[0]; my $a2 = $queryx[1]; my $a3 = $queryx[2]; while ($i <= $ss) { my $b1 = $basex[$i]; my $b2 = $basex[$i+1]; my $b3 = $basex[$i+2]; my $rr1 = (($a1) - ($b1)); my $rr2 = (($a2) - ($b2)); my $rr3 = (($a3) - ($b3)); my $r1 = sprintf "%.3f", (abs($rr1)); my $r2 = sprintf "%.3f", (abs($rr2)); my $r3 = sprintf "%.3f", (abs($rr3)); if (($r1 <= $j) && ($r2 <= $j) && ($r3 <= $j)) { my $r_sq1 = $r1 * $r1; my $r_sq2 = $r2 * $r2; my $r_sq3 = $r3 * $r3; my $rsum = ($r_sq1 + $r_sq2 + $r_sq3); #print FILEHANDLE "$a1\t$aa1\n"; print FILEHANDLE "$a1\t$a2\t$a3\t$r1\t$r2\t$r3\n"; } if ($k == $#queryx) { exit; } if ($i == $ss) { $k = $k + 1; $a1 = $queryx[$k]; $a2 = $queryx[$k+1]; $a3 = $queryx[$k+2]; print "$a1\t$a2\t$a3\n"; next; } $i++; } close (FILEHANDLE);

READMORE tags added by Arunbear


In reply to Can any one help me with my query by Anonymous Monk

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.