Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Usenet and arrays

by ergowolf (Monk)
on Mar 25, 2003 at 14:14 UTC ( [id://245687]=perlquestion: print w/replies, xml ) Need Help??

ergowolf has asked for the wisdom of the Perl Monks concerning the following question:

I have been doing some programming with the News::NNTPClient module. The documentation includes the example...
for (; $first <= $last; $first++) { print $c->article($first); }
I would prefer to use the foreach loop like this...
($first, $last) = ($newserver->group($newsgroup)); my @messages = ($first..$last); foreach (@messages) { print $c->article($_); }
My problem is that some message IDs don't exist and have no array pointer causing problems in my program. I thought I could get rid of the errors this way...
($first, $last) = ($newserver->group($newsgroup)); my @messages = ($first..$last); my $count = 0; foreach (@messages) { if ($_ = undef || "") { splice(@testlist, $count, 1); } $count++; } foreach (@messages) { print $c->article($_); }
Unfortunately, it doesn't capture the errors. It looks like $c->article($_) points to an array reference. How can I find the message IDs that are causing the problems?

Replies are listed 'Best First'.
•Re: Usenet and arrays
by merlyn (Sage) on Mar 25, 2003 at 14:35 UTC
      Randal:
      "Be sure to read my standard disclaimer if this is a reply."

      I read it, but I am waiting to hear back from my lawyers...

      On a more serious note, I have alot of respect for you. You do alot for the perl community and I apreciate you taking the time out to help me. I just want to say thank you.

      "I tend to stay away from the apparently abandoned News::NNTPClient, preferring the more-recently-updated Net::NNTP, in code like my rec.humor.funny reader."

      http://www.stonehenge.com/merlyn/UnixReview/col22.html

      "The return value tells how many articles are in the group, along with a minimum and maximum article number. We can use that to scan through all possible article numbers and dump them out. Let's do that with a foreach loop:

      foreach my $artnum ($low..$high) { my $art = $c->article($artnum) or next;

      If an article doesn't exist (perhaps a cancellation or a different expiration date), we skip over to the next article number."

      I am going to try this later today and read the rest of your article while I am crammed in a NYC subway car(sardin can) on the way home from work.

      I do have a followup question though. I looked up the module at CPAN and found....

      http://search.cpan.org/author/GBARR/libnet-1.13/Net/NNTP.pm

      listgroup ( GROUP ) Returns a reference to a list of all the active messages in GROUP, or the current group if GROUP is not specified.

      Will listgroup give me an array of all the active articles saving me the trouble of weeding out the bad articles?

      iguanodon:
      "I've used xover to fetch the article information into a hash keyed by message ID. That way you just end up with the articles you know are available. Note that the News::NNTPClient docs say that xover may not be supported, but in my experience this hasn't been a problem."

      What module did you use? Do you have a sample?
        Will listgroup give me an array of all the active articles saving me the trouble of weeding out the bad articles?
        If that's what it does (haven't looked or tried), and then what you're going to do is iterate over that list, I'd suggest staying with the interface I used in my program.

        In general, I prefer getting an iterator over getting an iterated list, because there's more overhead in having the called subroutine prepare the list, just to do what you could have done in the first place.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Usenet and arrays
by davorg (Chancellor) on Mar 25, 2003 at 14:26 UTC

    Something like this perhaps.

    ($first, $last) = ($newserver->group($newsgroup)); foreach ($first .. $last) { my $article = $c->article($_); print $article if $article; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Usenet and arrays
by dakkar (Hermit) on Mar 25, 2003 at 14:31 UTC

    Don't know Net::NNTPClient, but...

    The first two snippets are equivalent (the second one may occupy more memory since it build an array of the msgids)

    In the third one, you are saying: for each number between $first and $last, if that number is undef or the empty string...

    Since no number passes the test, @messages will not be altered (assuming that that @testlist should be @messages)

    -- 
            dakkar - Mobilis in mobile
    
Re: Usenet and arrays
by iguanodon (Priest) on Mar 25, 2003 at 14:43 UTC
    I've used xover to fetch the article information into a hash keyed by message ID. That way you just end up with the articles you know are available. Note that the News::NNTPClient docs say that xover may not be supported, but in my experience this hasn't been a problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://245687]
Approved by sschneid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found