Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

problem in for loop

by MVRS (Acolyte)
on May 16, 2013 at 02:09 UTC ( #1033754=perlquestion: print w/replies, xml ) Need Help??
MVRS has asked for the wisdom of the Perl Monks concerning the following question:

hi friends , here a perl script written by me to merge link files

my input(link) is as following

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

use strict; use warnings; my $JOB = $ARGV[0]; my $threshold = $ARGV[1]; my $fh = "/home/Desktop/merge/$JOB/src/link.txt"; open (INFILE_link,"<$fh") or die $!; my @file_array; while (my $line = <INFILE_link>) { chomp $line; my @line_array = split(/\s+/, $line); push (@file_array, \@line_array); } my $arraySize_link = scalar (@file_array); my $ix=1; my ($i, $a, $b, $c, $d, $x, $w, $y, $z, $ta, $tb, $tc, $td); $a= $b= $c= $d= $x= $w= $y= $z= $ta = $tb =$tc =$td= 0; #open (FILE,'>'."$fh"); for ( $i=0; $i<$arraySize_link; $i+=2) { $ta = $file_array[$i][2]; $tb = $file_array[$i][3]; $tc = $file_array[$i+1][2]; $td = $file_array[$i+1][3]; if ($a=$b=$c=$d=$x=$w=$y=$z = 0) { $a = $x = $ta ; $b = $y = $tb ; $c = $w = $tc ; $d = $z = $td ; last; } if ( ($ta-$a) < $threshold && ($tc-$c) < $threshold) { # copy tabcd to abcd $a = $ta ; $b = $tb ; $c = $tc ; $d = $td ; } else { print "$file_array[$i][0] $file_array[$i][2] $file_array[$i+2] +[3]\n$file_array[$i+1][0] $file_array[$i+1][2] $file_array[$i+3][3]\n +"; $a = $x = $ta ; $b = $y = $tb ; $c = $w = $tc ; $d = $z = $td ; } } # end of for loop if ($a=$b=$c=$d=$x=$w=$y=$z != 0) { print "$file_array[$i][0] $file_array[$i][2] $file_array[$i][3 +]\n$file_array[$i+1][0] $file_array[$i+1][2] $file_array[$i+1][3]\n"; }

My out put

link1 16 31245

link1 7419028 7450255

link2 13016 50386

link2 7432026 7469693

link3 31569 82019

link3 7450876 7501295

link4 53241 85039

link4 7472518 7513769

link5 82667 162535

link5 7511397 7591243

link6 85052 3527802

link6 7513770 6200895

link7 3519888 3535088

link7 6192981 6204356

link8 3531711 3568351

link8 6200982 6234981

link9 3537764 3670398

link9 6204393 6321680

link10 3585050

link10 6236328

please help me in fixing the error and in the last two lines the numbers were not getting in the output please help me in fixing the problem and also help me in fixing the code with out warnings

here in the output as perl toolic reply he is right , please somebody suggest me in fixing this problem how can i change the code to fix this problem of for loop

Replies are listed 'Best First'.
Re: Use of uninitialized value in concatenation (.) or string
by toolic (Bishop) on May 16, 2013 at 02:28 UTC
    You are trying to access an element which is beyond the last one in your array:
    $file_array[$i+3]
    You have 20 elements in the array (20 lines in your input), and the last time through the loop, $i=18, but $i+3=21.

    Tip #2 from the Basic debugging checklist : add print "$i\n" near the top of your loop.

Re: Use of uninitialized value in concatenation (.) or string
by Cristoforo (Curate) on May 16, 2013 at 03:27 UTC
    I think you have an error here:

    if ($a=$b=$c=$d=$x=$w=$y=$z = 0)

    That will never be true - it is just assigning 0 to all those variables. I think you meant to check that they are all zero. To do that, you would want this code:

    if (0 == grep $_ != 0, $a,$b,$c,$d,$x, $w,$y,$z) { say "all zero"; }
Re: Use of uninitialized value in concatenation (.) or string
by Anonymous Monk on May 16, 2013 at 05:56 UTC
    open (INFILE_link,"<$fh") or die $!;

    That would be better as:

    open my $INFILE_link, '<', $fh or die "Could not open '$fh' because: $ +!";


    while (my $line = <INFILE_link>) { chomp $line; my @line_array = split(/\s+/, $line); push (@file_array, \@line_array); }

    That would be better as:

    while ( <$INFILE_link> ) { push @file_array, [ split ]; }

    Or maybe:

    while ( my $line = <$INFILE_link> ) { push @file_array, [ split ' ', $line ]; }


    my ($i, $a, $b, $c, $d, $x, $w, $y, $z, $ta, $tb, $tc, $td); $a= $b= $c= $d= $x= $w= $y= $z= $ta = $tb =$tc =$td= 0;

    That might be better as:

    my ( $i, $a, $b, $c, $d, $x, $w, $y, $z, $ta, $tb, $tc, $td ) = ( 0 + ) x 13;

    Or maybe:

    $_ = 0 for my ( $i, $a, $b, $c, $d, $x, $w, $y, $z, $ta, $tb, $tc, +$td );
Re: problem in for loop
by hdb (Monsignor) on May 16, 2013 at 07:07 UTC

    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.

      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
Re: problem in for loop
by space_monk (Chaplain) on May 16, 2013 at 10:04 UTC
    # is this really what you meant? # this is assignment, not comparison == is comparison if ($a=$b=$c=$d=$x=$w=$y=$z = 0)
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033754]
Approved by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-07-16 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (333 votes). Check out past polls.

    Notices?