Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splicing one array modifies values of another?

Status
Not open for further replies.

darkreaper00

Technical User
Aug 5, 2002
23
US
I am pulling my hair out here, because @B should be static!

Code:
foreach my $chr (keys %hits) {

  foreach my $strand (keys %{$hits{$chr}}) {
    
    my %ref;

    ## sort by location on subject then location on query.

[COLOR=red]    my @A =  sort {$a->[2]<=>$b->[2] || $a->[0]<=>$b->[0]} @{$hits{$chr}{$strand}} ;[/color]

  #  my @B = @A;
[COLOR=red]    my @B =  sort {$a->[2]<=>$b->[2] || $a->[0]<=>$b->[0]} @{$hits{$chr}{$strand}} ;[/color]


    foreach $r (0..$#B){
      $ref{$B[$r][5]} = $r if ($B[$r][5]);
    }
    

    print REPORT "$chr $strand has ",$#A+1," hits.\n";

    print STDERR "$chr $strand \t";

  LOCATION: foreach my $i (0..$#A) {
      
    CONSIDER: foreach my $j ($i+1..$#A) {

	print REPORT "\$i=$i, \$j=$j,   $A[$i][3] <=> $A[$j][2], $A[$i][1] <=> $A[$j][0] ... ";

	if ( $A[$j][2] > $A[$i][3] + $thresh ) { 
	  print REPORT "0: fail: too far\n";
	  next LOCATION; }

	elsif ( (abs($A[$j][2] - $A[$i][3]) < $thresh) 
		&& (abs($A[$j][0] - $A[$i][1]) < $thresh) 
		&& (   ( ($A[$j][0]>=$A[$i][1]-200) && ($strand eq "plus") )
		       or
		       ( ($A[$j][0]<=$A[$i][1]+200) && ($strand eq "minus") )   )
		                              #b/c sometimes a slight overhang breaks a module.
	      ) {
	  

#	  foreach my $h ($ref{$A[$i][5]}..$ref{$A[$j][5]}) {
	  foreach my $h (0..$ref{$A[$j][5]}) {
# can't settle on the first value.  $ref-1 could be the same hit, ref{x-1} could be far, far away.  *shrug*

	    if ( ( ($strand eq "plus") && ($B[$h][0] < $A[$j][0]-20) && ($B[$h][1] > $A[$i][1]+20) && ($A[$j][0]-$A[$i][1]>40) ) 
	      || ( ($strand eq "minus") && ($B[$h][0] > $A[$j][0]+20) && ($B[$h][1] < $A[$i][1]-20) && ($A[$i][1]-$A[$j][0]>40) )
	       ) {
	               [COLOR=red]print REPORT "5: fail: insertion $B[$h][0], $B[$h][1] between $A[$j][0] and $A[$i][1] \n";[/color]
	               next LOCATION;
		 }
	  } 
	  
[COLOR=red]	  ($A[$i][1],$A[$i][3]) = ($A[$j][1],$A[$j][3]);
	  
	  splice (@A, $j, 1);[/color]
	  print REPORT "1: JOINED!\n";
	  last CONSIDER if ($#A < $j);
	  redo CONSIDER;
	}
#....
}


The problem is that I get the error 5, my values in @B are those that have been changed in @A, not the native values I wrote into @B specifically so I'd have a static copy to test against.

arg! Am I referencing something somewhere without realizing it?

Any help appreciated. Thanks.
 
If you give us some data to run with and an idea of what output we should get then maybe some of us can test it for you.
The code, as it stands, is too hairy for anyone to understand quickly and unless we can test it quickly with cut and paste, it's put's people off trying to answer.
Over to you ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top