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);

janitored by ybiC: Balanced  readmore> tags around longish codeblock, to avoid/reduce vertical scrolling.   Also retitle from less-than-descriptive "Can anyone help me what is the error in this script" for better site-search results.   Suggestions welcome for better retitling title.

Replies are listed 'Best First'.
Re: Can anyone help me what is the error in this script
by tlm (Prior) on Jun 03, 2005 at 12:26 UTC

    There were a lot of problems with your code. I've tried to clean it up (but didn't test it beyond perl -c). Let me know if it doesn't do what you want.

    The only halfway tricky thing my code does is that it avoids computing differences between query and base more than once by keeping a "sliding window" of differences in @r; the new element is always added to $r[ $i % 2 ], and then all elements are tested to make sure that they are not greater than $j. This can be optimized a lot more, at the expense of changing the structure of the code more than I wanted.

    #!/usr/bin/perl # This is a script which compares the query file and base with some # threshold given by the user use strict; use warnings; $| = 1; print "Enter the Threshold Value: \n"; my $j = <>; chomp $j; my $query_data = read_data( '10.asc.output' ); my $dir = '/home/users/dachs/perl_scripts/perlup'; #dir path where opendir my $dh, $dir or die "System can't open directory for reading\n +"; while (my $filename = readdir $dh) { next unless $filename =~ /\.asc.output$/i; my $base_data = read_data( $filename ); open my $out, ">$filename.cmp" or die "cannot open file for writing: $!"; # Creates an output fil +e my @r = map abs( $query_data->[ $_ ] - $base_data->[ $_ ] ), 0, 1; my $i = 2; while ( $i < @$query_data ) { $r[ $i % 3 ] = abs( $query_data->[ $i ] - $base_data->[ $i ] ); if ( $r[ 0 ] <= $j && $r[ 1 ] <= $j && $r[ 2 ] <= $j ) { my $matched = join "\t", @{ $query_data }[ $i-2..$i ]; print $out "Matched: $matched\n"; } $i++; } close $out; } closedir $dh; sub read_data { my $filename = shift; open my $in, $filename or die; my @data; while ( <$in> ) { push @data, $1 if /(\S+)\s+\S+/; # difficult to advise on regex # without knowing the details of # the data } close $in; return \@data; }

    the lowliest monk

      Thank you very much for your help especially tlm. The code which you have written made things clear to me. Thanks once again.
Re: Can anyone help me what is the error in this script
by NateTut (Deacon) on Jun 03, 2005 at 12:35 UTC
    I notice you have "use warnings;" commented out. Don't do that. The warnings are trying to tell you about potential problems in your code, I always use it. I won't run code that doesn't run warning free.

    Also you might want to consider adding "use diagnostics;" to your code. This will give you a more in depth explanation of errors and warnings in your code.
Re: Can anyone help me what is the error in this script
by thcsoft (Monk) on Jun 03, 2005 at 11:47 UTC
    ... and yet another time:
    #!/usr/bin/perl -wd
    you got a brain. just use it! :D

    language is a virus from outer space.