The edited version of the code contains ## comments where I made changes or inserted comments.

Note in particular that the array @Y2 was renamed to @y2! That (presumed) typo was picked up by use strict;.

I don't see any particular issue with the while loop. Perhaps you could describe the problem that you are having with it?

#use GD; use strict; use warnings; my @x1; my @y1; my $max1; my $min1; my $lines = LoadFile ("fwd.stb", \@x1, \@y1, \$min1, \$max1); # Define constants my $X1start = 50+20; # X-axis my $Y1start = 300; # X-axis my $X1end = 450+$lines-400; # X-axis my $Y1end = 300; # X-axis my $X2start = 50+20; my $Y2start = 100; my $X2end = 50+20; my $Y2end = 300; # create a new image my $im = new GD::Image($X1end+50,$Y2end+100); # allocate some colors my $white = $im->colorAllocate(255,255,255); my $green = $im->colorAllocate(0,255,0); my $black = $im->colorAllocate(0,0,0); my $red = $im->colorAllocate(255,0,0); my $blue = $im->colorAllocate(0,0,255); # set the caption for the graph $im->string(gdLargeFont,int($lines/2)-50,20,"ENERGY GRAPH",$black); # draw the y-axis & x-axis $im->line($X2start,$Y2start,$X2end,$Y2end,$black); $im->line($X1start,$Y1start,$X1end,$Y1end,$black); # set the caption for x-axis and y-axis $im->string(gdLargeFont,int($lines/2)-50,335,"NUCLEOTIDE POSITION",$bl +ack); $im->stringUp(gdLargeFont,5,260,"FREE ENERGY(delta G)",$black); $im->string(gdSmallFont,int($lines/2)+100,20," Genomic Sequence ----- +--",$red); $im->string(gdSmallFont,int($lines/2)+100,40," Shuffled Sequence ----- +--",$blue); my @x2; my @y2; ## Was @Y2 - probably a bug. Caught by use strict my $min2; my $max2; $lines = LoadFile ("total_shuffle.stb", \@x1, \@y1, \$min2, \$max2); my $min = $min1 <= $min2 ? $min1 : $min2; my $max = $max1 >= $max2 ? $max1 : $max2; my @y = @y1; my @x = @x1; graph(); @y = @y2; @x = @x2; my $png_data = $im->png; open (DISPLAY,"| display -") || die; #open (DISPLAY,">image.jpg") || die; binmode DISPLAY; print DISPLAY $png_data; close DISPLAY; sub graph { # set min and max energy my $minEnergy = $min; my $maxEnergy = $max; # set the length of x-axis and y-axis my $xLen = $X1end-$X1start; my $yLen = $Y2end-$Y2start; # Scaling the values my $Yscale = $yLen/(abs($minEnergy)-abs($maxEnergy)); my $scale = ($minEnergy-$maxEnergy)/5; my $incEnergy = $maxEnergy; while ($incEnergy > $minEnergy) { ##### Prob loop#### my $x1 = $X1start-2; my $y1 = int(((abs($incEnergy)-abs($maxEnergy))*$Yscale)+$Y2st +art); my $x2 = $X1start+2; $im->line($x1,$y1,$x2,$y1,$black); my $displayEnergy = sprintf("%2.1f",$incEnergy); $im->string(gdSmallFont,$x1-35,$y1-10,$displayEnergy,$black); $incEnergy = $incEnergy+$scale; } my $count = 0; ## $count was not declared my $len = @x; # set the length of nucleotide my $posSkip = int(@x/10); # mark 10 points on the position axis my $skipPixel = $xLen/$len; # If the length of the fragment is les +ser than the x-axis for(my $i=0;$i<$len-1;$i++) { my $x1 = int(($i*$skipPixel)+$X1start); my $y1 = int(((abs($y[$i])-abs($maxEnergy))*$Yscale)+$Y2start) +; my $x2 = int((($i+1)*$skipPixel)+$X1start); my $y2 = int(((abs($y[$i+1])-abs($maxEnergy))*$Yscale)+$Y2star +t); ## graph is only called once in this code so $call can only ev +er be undef ##if($call == 0) { $im->line($x1,$y1,$x2,$y2,$red); ##} else { ## $im->line($x1,$y1,$x2,$y2,$blue); ##} $count++; if($count == $posSkip) { $x1 = int(($i*$skipPixel)+$X1start); $y1 = $Y1end-2; $x2 = int(($i*$skipPixel)+$X1start); $y2 = $Y1end+2; ## graph is only called once in this code so $call can onl +y ever be undef ##if($call == 0) { $im->line($x1,$y1,$x2,$y2,$black); $im->string(gdSmallFont,$x1,$y1+10,$i+1,$black); ##} $count = 0; } } ## graph is only called once in this code so $call can only ever b +e undef ## $call++; } ## Common file reading code in sub sub LoadFile { my ($filename, $x, $y, $min, $max) = @_; ## Three parameter open and check result open FILE1,'<', $filename or die "Failed to open $filename: $!"; ## Remove $i from while loop - [-1] is last element while(<FILE1>) { my @value = split(/\s+/,$_); next if 2 != @value; # Skip if bad input line - could die or w +arn push @x, $value[0]-7; push @y, $value[1];
if(@$x1) { $$max = $y->[-1] if $y->[-1] > $max; $$min = $y->[-1] if $y->[-1] < $min;
if(@$x) { $$max = $y->[-1] if $y->[-1] > $$max; $$min = $y->[-1] if $y->[-1] < $$min; } else { $$min = $$max = $y->[-1]; } } return $.; ## Number of lines }

Update bugs fixed - see replies


DWIM is Perl's answer to Gödel

In reply to Re: Prob with 'while' loop and subroutine calling by GrandFather
in thread Prob with 'while' loop and subroutine calling by cool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.