Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

GD, arrays, and Polygons

by neilwatson (Priest)
on Mar 23, 2003 at 17:15 UTC ( [id://245290]=perlquestion: print w/replies, xml ) Need Help??

neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, please consider this code:
2 use strict; 3 use GD; 4 5 my $length = 2; #number of repeats 6 my $image = new GD::Image($length*37+1,13); 7 my $white = $image->colorAllocate(200,200,200); 8 #$image->transparent($white); 9 my $yellow = $image->colorAllocate(255,255,0); 10 my $black = $image->colorAllocate(0,0,0); 11 my ($x, @para1, @para2); 12 13 while ($x <= $length-1){ 14 $para1[$x] = new GD::Polygon; 15 $para1[$x]->addPt(0+$x*12,0); 16 $para1[$x]->addPt(12+$x*12,12); 17 $para1[$x]->addPt(24+$x*12,12); 18 $para1[$x]->addPt(12+$x*12,0); 19 20 $para2[$x] = new GD::Polygon; 21 $para2[$x]->addPt(13+$x*12,0); 22 $para2[$x]->addPt(25+$x*12,12); 23 $para2[$x]->addPt(37+$x*12,12); 24 $para2[$x]->addPt(25+$x*12,0); 25 26 $x++; 27 } 28 29 while ($x >= 0){ 30 $image->filledPolygon($para1[$x], $black); 31 $image->filledPolygon($para2[$x], $yellow); 32 33 $x--; 34 } 35 print $image->png();

I'm am receiving this error Can't call method "length" on an undefined value at line 30

Can anyone offer some advice as to the cause? Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: GD, arrays, and Polygons
by pfaut (Priest) on Mar 23, 2003 at 17:25 UTC

    You execute the first loop twice with $x set to 0 and 1. You build entries 0 and 1 in @para1 and @para2. When you leave the first loop, $x = 2. You then attempt to execute the second loop with $x set to 2 but you never defined that entry in the arrays. Move the decrement to the beginning of the loop.

    while ($x >= 0){ $x--; $image->filledPolygon($para1[$x], $black); $image->filledPolygon($para2[$x], $yellow); }
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Ack, I'm spending to much time performing Windows support. Thanks.

      Neil Watson
      watson-wilson.ca

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://245290]
Approved by pfaut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found