Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Programmatically building named anchors to warp to sections

by mr_mischief (Monsignor)
on Aug 15, 2007 at 01:33 UTC ( #632641=note: print w/replies, xml ) Need Help??


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).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-01-26 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?