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).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|