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

[FWP] Puzzle - Can you find the bug faster than I did?



This code had worked before with the previous data values (see the 
end of this message), but the data had changed and then the code was 
given to me to work with and ... it sure didn't seem to be working 
now.  Maybe it never worked and no one had noticed; maybe something 
had changed.

The goal of the program is to read and edit an existing log file on the fly,
like sed  would.

The input data file looked like:

g1840610        417     417     { 1-417 }       Initial 1.3
g1840611        1       1       { 1-1 } Initial 1.3
g1840611        1       1       { 1-1 } Length  1.3
g1840611        1       1       { 1-1 } Removed 1.3
g1840612        421     421     { 1-421 }       Initial 1.3


The idea is to read (and print) each line until you come to one with a
new ID (e.g. g1840610), then insert a new entry between the last line of the
previous ID's data and the first line of the new ID's data and
continue on.

I had been trying to track down why the code didn't seem to be working.
Nothing was being added to the log file and my traces were printing
"part of same ID" every time.

Note that I didn't write this code, and it's unchanged except for the
addition of the trace() statements and the fact that I rearranged and
edited the comments (which were in all CAPS...)

      while (<FILE>) {
	    $i++;
	    ($field1,$rest) = split(/\t/);
         trace(1, "found $field1\n");
	    if (($field1 == $prev_clone) || ($i == 1)) {
	        # current line is part of the current id
	        trace(1, "part of same ID\n");
	        push(@temp_clone,$_);
	    }
	    else {
	 		# current line is new clone id
	        trace(1, "part of new clone\n");
             # print out previous clone id information
	        for ($k=0; $k <= $#temp_clone; $k++) {
	    	    print WRITE $temp_clone[$k];
	        }

	        # if previous clone id is to be removed
	        if (defined $clone_list{$prev_clone}) {
		        &remove_clone($prev_clone);
	        }
	        undef @temp_clone;
	        push(@temp_clone,$_);
	    }
	    $prev_clone = $field1;
     }


Do you see the bug?

(When I found it, it took me much too long to see it, considering)

HINT: The previous data values looked like this

7500123N        417     417     { 1-417 }       Initial 1.3
7500124N        1       1       { 1-1 } Initial 1.3
7500124N        1       1       { 1-1 } Length  1.3
7500124N        1       1       { 1-1 } Removed 1.3
7500138N        421     421     { 1-421 }       Initial 1.3
-- --
        |\      _,,,---,,_       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 mail with body "unsubscribe" to
==== fwp-request@technofile.org