[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] small(?) puzzler (misdirection)



Background:
Often, puzzles work by misdirection. You see what you expect to see,
which is often what the programmer expected to have written.
But you don't see what the programmer _actually_ wrote, which is similar
but not the same thing...

This was a real example at work a few weeks back
Because we knew what we were supposed to see, the bug took an
embarrassingly long time to find
I finally found it when I moved (physically) from one side of the desk
to the other and looked back at the code from (literally) another angle

This is the part of the original script that gave us trouble
The original script had some extra functionality


# This script should generats output of the form
#
# T1234 \t Top hit (gi123456) \t score
#
# If there were "No hits found" it should print
#
# T1234: no hits found
#
# Data looks like:
#
# ------------------------------------
# Query= T181
#          (1278 letters)
#
# gi|2138341|gb|U92802|RNU92802 Rattus norvegicus orphan...  2533  0.0

	$/ = "------------------------------------";

	$hitless = 0;

	(<DATA>);

	while(<DATA>) {
		$query = $seq = $score = "";
		@lines = split(/\n/);
		for $line (@lines) {
			if ($line =~ /^Query=\s+(\w+)/) {
				$query = $1;
			}
			if ($line =~ /^gi\|(\d+)/) {
				$seq = $1;
				@line = split(/\s+/, $line);
				$score = $line[-2];		#ie, the second to the last element
				last;
			}
			if (/No hits found/) {
				print "$query:  no hits found\n";
				$hitless++;
				last;
			}
		}
		if ($seq ne "") {
			print "$query\t$seq\t$score\n";
		}
	}

print "$hitless queries failed to hit anything\n";

# Expected (desired) results:
#
#
#    T181   2138341 2533
#    T43:  no hits found
#    T342   455487  4940
#    1 queries failed to hit anything
#
#
# But this prints
#   T181    2138341 2533
#   :  no hits found
#   T342    455487  4940
#   1 queries failed to hit anything
#
# We never see the Query number for the "no hits" records
#
#

__END__
------------------------------------
Query= T181
          (1278 letters)

gi|2138341|gb|U92802|RNU92802 Rattus norvegicus orphan...  2533  0.0

------------------------------------
Query= T43
          (542 letters)

Database: nt
            411,225 sequences; 999,688,133 total letters

  ***** No hits found ******

------------------------------------
Query= T342 cannabinoid receptors
          (2492 letters)

gi|455487|dbj|D21062|MUSGPCR21 Mouse mRNA for G...   4940  0.0

-- --
        |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
  ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
       |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
      '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.com

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe