Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Can any one help me with my query
by tlm (Prior) on Jun 21, 2005 at 13:14 UTC

    I mean this constructively: your question makes absolutely no sense to me, and I can't help but wonder if there is a connection between this apparent difficulty with formulating the question and your stated difficulty with the program.

    It happens to me all the time that in the process of formulating a question as clearly as possible (usually in the course of writing a post to a forum like PM) I end up discovering the answer by myself.

    My point is that your problem may have less to do with Perl than with just thinking through exactly and unambiguously what you are trying to do. Correct specification is half the battle.

    the lowliest monk

Re: Can any one help me with my query
by anonymized user 468275 (Curate) on Jun 21, 2005 at 12:36 UTC
    I would say step one is to define your "difference" algorithm more clearly (in english rather than code is ok) rather than trying to give example data on its own.
      Thanks very much for replying. I have sorted my problem by just making i=0 where next has been used. I am very much happy at least you replied.