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

Hello Monks, I am back again with another problem. This script is executing fine without any error. The output file which it create with ".asc.output.cmp" are empty except one file which is 6.asc.output (@basex) containing accurate result. I feel somewhere I am doing wrong which is causing this. This script is written to compare the query file with all the other files in the directory to check for any nearby values with some threshold value entered by the user. I am very sorry for bothering you all.
#!/usr/bin/perl # This is a script which compares the query file and base with some th +reshold given by the user #use warnings; use strict; print "Enter the Threshold Value: \n"; my $j= <STDIN>; my $filename1 = "10.asc.output"; my (%hash, @queryx, @queryy); open (FILE1, $filename1) or die "can't open $filename1: $!\n"; while(<FILE1>) { if ($_ =~ /(\S+)\s+(\S+)/) { $hash{$1} = $2; push(@queryx,$1); push (@queryy, $2); } } close(FILE1); my ($filename, $i, @basex, @basey, $folder, $a1, $a2, $a3, $b1, $b2, $ +b3, $r1, $r2, $r3, $rr1, $rr1, $rr2, $rr3); $folder = '/home/users/dachs/perl_scripts/perlup'; #folder path where opendir(MYDIR, $folder) || die "System cant open directory reading for +\n"; while ($filename = readdir MYDIR){ next unless $filename =~ /\.asc.output$/i; #Check for the file .as +c #undef %hash; #undef @basex; #undef @basey; open FILE, "$filename" or die "can't open $filename: $!\n"; while(<FILE>){ if ($_ =~ /(\S+)\s+(\S+)/) { $hash{$1} = $2; push(@basex,$1); push (@basey, $2); } } close (FILE); open(FILEHANDLE, ">$filename.cmp") or die "cannot open file for readin +g: $!";#Creates an output file while ($i <= $#basex) { $a1 = $queryx[$i]; $a2 = $queryx[$i+1]; $a3 = $queryx[$i+2]; $b1 = $basex[$i]; $b2 = $basex[$i+1]; $b3 = $basex[$i+2]; $rr1 = (($a1) - ($b1)); $rr2 = (($a2) - ($b2)); $rr3 = (($a3) - ($b3)); $r1 = abs($rr1); $r2 = abs($rr2); $r3 = abs($rr3); $i++; print $rr1, "\n"; if (($r1 <= $j) && ($r2 <= $j) && ($r3 <= $j)) { print FILEHANDLE "Matched: ", $a1, "\t", $a2, "\t", $a3, "\n"; #$count = $count + 1; print $r1, $r2, $r3, "\n"; } } close (FILEHANDLE); } closedir(MYDIR);

Replies are listed 'Best First'.
Re: Why output is appending in each file??
by Joost (Canon) on Jun 02, 2005 at 13:51 UTC
Re: Why output is appending in each file??
by fishbot_v2 (Chaplain) on Jun 02, 2005 at 14:53 UTC

    undef takes a scalar, not a list. (see perldoc -f undef)

    use strict; use warnings; my %hash = ( foo => 'bar' ); my @index = qw( baz bong ); my @a2 = qw( a b c ); undef %hash, @index, @a2; print %hash, @index, @a2; __END__ # prints -> baz bong a b c

    When I do this with warnings on, though, I get "Useless use of private array in void context" warnings, since the remainder of that expression does nothing with the variables. You should be seeing those too, no?

    Rather than depending on yourself to clear out values at the top of each loop, I strongly recommend just making them lexicals, and use strict. You avoid this whole class of bugs.

    Additionally, I am not sure what %hash and @index are doing in there. You populate them, then don't use them, then clear them.

Re: Why output is appending in each file??
by Ultra (Hermit) on Jun 02, 2005 at 14:11 UTC
    I didn't quite understood what you're trying to do, as this didn't help. The error is the way you empty the arrays/hashes (undef), so instead of
    undef %hash,@index,@a2;
    maybe you should try:
    undef %hash; undef @index; undef @a2;
    which will make perl forget about those variables, or
    %hash=(); @index=(); @a2=();
    which will make perl empty the contents of those variables
    Dodge This!
      Thank you very much for everyone. My problem has been solved with your suggestions.