Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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.

Replies are listed 'Best First'.
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?
[Corion]: LanX: Ow ;)
Discipulus manual work: i just tell the same to my boss: every time the quick solution is to assign some manual data entry task to my group.. because we have not direct access to many databases here..
[LanX]: point is: in high speed trade each bank has to remember what he has to get from the others... so dresdner got billed for losses but couldn't claim gains
Discipulus is this the IT?
[Corion]: Discipulus: Well, in many cases it doesn't make sense to build an interface and complicated program just to enter 20 rows into a database ;) But yes, automating data imports should pay off in the long run
[LanX]: Choroba: this happened before I joined, was still in uni, but my boss was summoned to the CEO of the second biggest German bank at that time and could only say " I told them its not ready" ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2017-03-29 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.