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


In reply to Re: Can anyone help me what is the error in this script by tlm
in thread Empty output files by cybersmithuk

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.