Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Bad index while coercing array into hash

by rbc (Curate)
on Sep 26, 2002 at 20:37 UTC ( #201047=perlquestion: print w/ replies, xml ) Need Help??
rbc has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am stumped. What am I doing wrong here.
#!/usr/bin/perl -w use lib ( '../Vector2D' ); use Vector2D; my $POLYGONS_MAX_Y = 11; my $A = new Vector2D( 2, 3 ); my $B = new Vector2D( 7, 1 ); my $C = new Vector2D( 13, 5 ); my $D = new Vector2D( 13, 11 ); my $E = new Vector2D( 7, 7 ); my $F = new Vector2D( 2, 9 ); my @AB = [ $A, $B ]; my @BC = [ $B, $C ]; my @CD = [ $C, $D ]; my @DE = [ $D, $E ]; my @EF = [ $E, $F ]; my @FA = [ $F, $A ]; my @polygon = ( @AB, @BC, @CD, @DE, @EF, @FA ); my @ET; # global Edge Table # each entry in the ET contains the Ymax coordinate of the edge, # the x cooridnate of the bottom endpoint Xbot and the x increment # used in the stepping from one scan lime to the next 1/m for my $edge (@polygon) { my $y = ( @{$edge}->[0]->gety() < @{$edge}->[1]->gety() ) +? @{$edge}->[0]->gety() : @{$edge}->[1]->gety() ; my $Ymax = ( @{$edge}->[0]->gety() >= @{$edge}->[1]->gety() ) +? @{$edge}->[0]->gety() : @{$edge}->[1]->gety() ; my $Xbot = ( @{$edge}->[0]->gety() < @{$edge}->[1]->gety() ) +? @{$edge}->[0]->getx() : @{$edge}->[1]->getx() ; my $invSlope = &calcOneOverSlope($edge); my $rec = { Ymax => $Ymax, Xbot => $Xbot, invSlope => $invSlope }; print "$y -> $Ymax | $Xbot | $invSlope\n"; push( @{$ET[$y]}, $rec ); } for my $i (1..$POLYGONS_MAX_Y) { if ( $ET[$i] ) { print "ET[$i] = "; for my $r ( ${ET[$i]} ) { print "{" . $r->{Ymax} . "|" . $r->{Xbot} . "| +" . $r->{invSlope} . "}" ; } print "\n"; } else { print "ET[$i] = NULL\n"; } } sub calcOneOverSlope { my $edge = shift; my $y = @{$edge}->[0]->gety()-@{$edge}->[1]->gety(); my $x = @{$edge}->[0]->getx()-@{$edge}->[1]->getx(); if ( $y == 0 ) { return undef; } return $x/$y; }
When I execute this I get the following output and error message.
$ ./buildET.pl 
1 -> 3 | 7 | -2.5
1 -> 5 | 7 | 1.5
5 -> 11 | 13 | 0
7 -> 11 | 7 | 1.5
7 -> 9 | 7 | -2.5
3 -> 9 | 2 | 0
Use of uninitialized value in concatenation (.) or string at ./buildET.pl line 49.
Use of uninitialized value in concatenation (.) or string at ./buildET.pl line 49.
Bad index while coercing array into hash at ./buildET.pl line 49.
ET(1] = 
I had hoped that the output would look like this ...
ET(1] = {3 | 7 | -2.5} {5 | 7 | 1.5}
ET(2] = NULL
ET(3] = {9 | 2 | 0}
ET(4] = NULL
ET(5] = {11 | 13 | 0}
ET(6] = NULL
ET(7] = {11 | 7 | 1.5} {7 -> 9 | 7 | -2.5}
ET(8] = NULL
...
ET(20] = NULL
Line 49 is this line ...
  print "{" . $r->{Ymax} . "|" . $r->{Xbot} . "|" . $r->{invSlope} . "}" ;
Thanks

Edited: ~Fri Sep 27 15:05:17 2002 (GMT) by footpad: s/<PRE>/<CODE>/; in last code line, per Consideration.

Comment on Bad index while coercing array into hash
Select or Download Code
Re: Bad index while coercing array into hash
by fglock (Vicar) on Sep 26, 2002 at 20:46 UTC
        for my $i (1..$POLYGONS_MAX_Y) {

    - perl arrays start in "zero" by default. Try:

        for my $i (0..$POLYGONS_MAX_Y - 1) {
      Thanks for the reply.
      I tried it out now my output looks like so ...
      1 -> 3 | 7 | -2.5 1 -> 5 | 7 | 1.5 5 -> 11 | 13 | 0 7 -> 11 | 7 | 1.5 7 -> 9 | 7 | -2.5 3 -> 9 | 2 | 0 ET(0] = NULL Use of uninitialized value in concatenation (.) or string at ./buildET +.pl line 49. Use of uninitialized value in concatenation (.) or string at ./buildET +.pl line 49. Bad index while coercing array into hash at ./buildET.pl line 49. ET(1] =
Re: Bad index while coercing array into hash
by chromatic (Archbishop) on Sep 26, 2002 at 23:21 UTC

    You're looping over @ET 11 times when it only contains 6 elements.

Re: Bad index while coercing array into hash
by rbc (Curate) on Sep 26, 2002 at 23:44 UTC
    Thanks be to the Gods for Data::Dumper.
    Useing Data::Dumper's Dumper() sub should me the error of my ways!

    I needed to be doing this ...
    for my $i (1..$POLYGONS_MAX_Y) { if ( $ET[$i] ) { print "ET[$i] = "; for my $list ( ${ET[$i]} ) { for my $r ( @{$list} ) { print "{" . $r->{Ymax} . "|" . $r->{Xbot} . "|" . $r->{invSlope} . "}" ; } } print "\n"; } else { print "ET[$i] = NULL\n"; } }
    Also thank those monks that replied to my question.

    Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-21 04:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (103 votes), past polls