in reply to Programmatically building named anchors to warp to sections

I just submitted this a couple of minutes ago:
my @p_results = ( { 'title' => 'Package' }, { 'title' => 'Paddle' }, { 'title' => 'Peck' }, { 'title' => 'Pessimist' }, { 'title' => 'Pickle' }, { 'title' => 'Piston' }, { 'title' => 'Pocket' }, { 'title' => 'Pout' }, { 'title' => 'Pride' }, { 'title' => 'Promise' }, { 'title' => 'Pucker' }, { 'title' => 'Putter' }, ); my @updates = qw( a e i o u ); my $i = 0; my $new_section; foreach my $item ( @p_results ) { if ( substr( $item->{'title'}, 1, 1 ) eq $updates[$i] ) { print "\n-= P" . $updates[$i] . " =-\n\n"; $i++; } print $item->{'title'} . "\n"; }

Better yet is this:
use strict; use warnings; my @p_results = ( { 'title' => 'Package' }, { 'title' => 'Paddle' }, { 'title' => 'Peck' }, { 'title' => 'Pessimist' }, { 'title' => 'Pickle' }, { 'title' => 'Piston' }, # { 'title' => 'Pocket' }, { 'title' => 'Pout' }, { 'title' => 'Pride' }, { 'title' => 'Promise' }, { 'title' => 'Pucker' }, { 'title' => 'Putter' }, ); my @updates = qw( a e i o u ~ ); my $i = 0; foreach my $item ( @p_results ) { if ( substr( $item->{'title'}, 1, 1 ) ge $updates[$i] ) { print "\n-= P" . $updates[$i] . " =-\n\n"; $i++; } print $item->{'title'} . "\n"; }

This new one is better in two ways. It handles the case that you never hit exactly the letter your header contains. It also doesn't have an extra variable that's never used. I caught that without "use warnings;", but I added those to the second one after I caught it.

See if you can figure out really quickly why there's an extra character in the @updates array the second time around.

Update 2: fixed a tpyo (in the exposition, not in the code).