in reply to Why is that a hash slice operand to grep adds keys to the hash (if the slice is bigger than the orig set of keys) ?

This also happens with code one of my friends is maintaining.
On his machine (NT?), indexing an array beyond its 'top' element
extends the array, and so a loop (which used to run fine) never
terminates:

my @main = ( "<UL>\n", "<!--begin-->\n", "</UL>\n" ); &getboardinfo(); sub getboardinfo { my($tnum); $msgcount = 0; $lastmsg = 0; $lastfu = 0; $fucount = 0; print STDERR "Going into loop: main is @main\n"; print STDERR "main has ", $#main + 1, " elements.\n"; for( $j=0; $j<=@main; $j++ ) { print STDERR "Loop! j is $j main[j] is $main[$j] main[j+2] is + $main[$j+2]\n";
An illustration of the problem lies in the code above.
The reference to $main[$j+2] extends the array, and so
the loop goes on forever.
if( $main[$j] =~ /<!--top: (.*)-->/ ) { $msgcount++; $lastmsg = $1; } elsif( $main[$j] =~ /<!--ins: (.*)-->/ ) { $tnum=$1; if( ( $main[$j+1] =~ /<!--end:/ ) &&( $main[$j+2] =~ /<!--end:/ ) ) { $lastfu = $tnum; $fucount++; } } } print STDERR "Done with loop\n"; return 0; }

Of course, the original writer should probably have found
a better way to write this loop (maybe by using foreach()).

Rob

  • Comment on Re: Why is that a hash slice operand to grep adds keys to the hash (if the slice is bigger than the orig set of keys) ?
  • Select or Download Code