in reply to problem in for loop

Your code looks rather complicated but it is difficult to guess what you want to achieve without knowing more about the underlying problem you want to solve. For example, I find the line

if ( ($ta-$a) < $threshold && ($tc-$c) < $threshold)

strange as I would expect some kind of abs($ta-$a) < $threshold instead. But without knowing more detail it is difficult to comment more specifically.

Replies are listed 'Best First'.
Re^2: problem in for loop
by MVRS (Acolyte) on May 16, 2013 at 07:24 UTC

    Thankyou very much for the response

    for example

    input file

    link1 myco1 x y

    link1 myco2 w z

    link2 myco1 a b

    link2 myco2 c d

    .

    '

    '

    if (y-a)&&(z-c)< threshold

    print as following

    link1 x b

    link1 w d

      Not sure about the else branch. If my assumption of printing the unchanged data is correct, then there are further simplifications possible.

      use strict; use warnings; my $threshold = 1000; # being lazy: my @file_array = map { [ split /\s+/ ] } <DATA>; for( my $i=0; $i<@file_array-2; $i+=2 ) { if( $file_array[$i ][3]-$file_array[$i+2][2] < $threshold and $file_array[$i+1][3]-$file_array[$i+3][2] < $threshold ) { print "$file_array[$i ][0] $file_array[$i ][2] $file_array[$ +i+2][3]\n"; print "$file_array[$i+1][0] $file_array[$i+1][2] $file_array[$ +i+3][3]\n"; } else { # not sure print "$file_array[$i ][0] $file_array[$i ][2] $file_array[$ +i ][3]\n"; print "$file_array[$i+1][0] $file_array[$i+1][2] $file_array[$ +i+1][3]\n"; } } print "$file_array[-2][0] $file_array[-2][2] $file_array[-2][3]\n"; print "$file_array[-1][0] $file_array[-1][2] $file_array[-1][3]\n"; __DATA__ link1 myco1 16 13013 color=chr1 link1 myco2 7419028 7432025 color=chr1 link2 myco1 13016 31245 color=chr1 link2 myco2 7432026 7450255 color=chr1 link3 myco1 31569 50386 color=chr1 link3 myco2 7450876 7469693 color=chr1 link4 myco1 53241 82019 color=chr1 link4 myco2 7472518 7501295 color=chr1 link5 myco1 82667 85039 color=chr1 link5 myco2 7511397 7513769 color=chr1 link6 myco1 85052 162535 color=chr1 link6 myco2 7513770 7591243 color=chr1 link7 myco1 3519888 3527802 color=chr10 link7 myco2 6192981 6200895 color=chr10 link8 myco1 3531711 3535088 color=chr10 link8 myco2 6200982 6204356 color=chr10 link9 myco1 3537764 3568351 color=chr10 link9 myco2 6204393 6234981 color=chr10 link10 myco1 3585050 3670398 color=chr10 link10 myco2 6236328 6321680 color=chr10

        nope sorry its not working its printing the same output but not merging the links i want the links to be merged