[reordered]
That's precisely what I needed to do.
Then the problem was not with Perl being hard to understand at all.
The problem was simply a programmer's logic error.
No. I bought the camel book 15 months ago and am not mature with the
syntax.
Not in my Perl programs.
$_ is simply the common default variable.
My programs never use the default, I always give them an explicit variable.
In my production code, I use $_ only in map() and grep().
....
So in my code, that would instead be:
foreach my $article ( @xover ) {
if ($article->[$from_offset] eq 'George <
[email protected]>') {
print STDOUT "got a match\n";
}
}
Look Ma! No $_!
#!/usr/bin/perl
use strict;
use warnings;
use Net::NNTP ();
use constant NUMBER_OF_ARTICLES => 100;
use constant GROUP_NAME => 'comp.lang.perl.misc';
use constant SERVER_NAME => 'news.individual.net';
use constant NNTP_DEBUG => 0;
my $nntp = Net::NNTP->new(SERVER_NAME, 'Debug' => NNTP_DEBUG) or die;
my $USER = '';
my $PASS = '';
$nntp->authinfo($USER,$PASS) or die $!;
my($article_count, $first_article, $last_article) =
$nntp->group(GROUP_NAME) or die;
# Which XOVER fields contain Subject: and From:?
my $count = 0;
my %xover_fmt = map( ($_, $count++), @{ $nntp->overview_fmt or die} );
die unless exists $xover_fmt{'Subject:'};
my $subject_offset = $xover_fmt{'Subject:'};
my $from_offset = $xover_fmt{'From:'};
my(@xover, $start_article);
RETRIEVE: while ($#xover+1 < NUMBER_OF_ARTICLES and $last_article >=
$first_article) {
# How many articles do we need? Stop retrieving if we have enough
my $articles_required = NUMBER_OF_ARTICLES - ($#xover+1) or last
RETRIEVE;
# Fetch overview information for the articles
$start_article = $last_article - ($articles_required-1);
$start_article = $start_article > $first_article ? $start_article :
$first_article;
my $xover_query = $start_article == $last_article ?
$start_article :
[$start_article, $last_article];
my $xover_ref = $nntp->xover($xover_query) or die;
# Store headers for the articles we've retrieved
foreach (sort {$b <=> $a} keys %$xover_ref) {
push @xover, $xover_ref->{$_};
}
} continue {
# Move the pointer forward to fetch previous articles
$last_article = $start_article - 1;
}
foreach ( @xover ) {
if ($_->[$from_offset] eq 'George <
[email protected]>') {
print STDOUT "got a match\n";
}
}
foreach my $counter ( @xover ) {
if ($counter->[$from_offset] eq 'George <
[email protected]>')
{
print STDOUT "'Look Ma! No \$_!'\n";
}
}
# Disconnect from the NNTP server
$nntp->quit;
# perl script3.pl
C:\MinGW\source>perl script3.pl
got a match
got a match
got a match
got a match
got a match
got a match
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
C:\MinGW\source>
Thanks, Tad. Please never accuse me of having an inability to learn.
--
George
This young century will be liberty's century.
George W. Bush
Picture of the Day
http://apod.nasa.gov/apod/