Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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).

In reply to Re: Programmatically building named anchors to warp to sections by mr_mischief
in thread Programmatically building named anchors to warp to sections by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (6)
    As of 2021-01-27 08:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?