OO Perl, iterators

Discussion in 'Perl Misc' started by Gregory Toomey, Oct 31, 2005.

  1. I'm a bit confused about the Perl OO syntax. From my reading "foreach" acts
    as an iterator.

    Any somebody tell me in the standard example below:
    - how to count the total number of $query->answer
    - how to access the i'th answer; $query->answer[$i] does not work
    - how to access a random answer

    thanks
    gtoomey
    --
    use Net::DNS;
    my $res = Net::DNS::Resolver->new;
    my $query = $res->query("example.com", "NS");

    if ($query) {
    foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
    print $rr->nsdname, "\n";
    }
    }
    Gregory Toomey, Oct 31, 2005
    #1
    1. Advertising

  2. Gregory Toomey

    Anno Siegel Guest

    Gregory Toomey <> wrote in comp.lang.perl.misc:
    > I'm a bit confused about the Perl OO syntax. From my reading "foreach" acts
    > as an iterator.


    It is a loop construct. An iterator is something that produces
    values to iterate over, like <> or "each" in Perl.

    > Any somebody tell me in the standard example below:


    For all of these, do

    my @answers = $query->answer;


    > - how to count the total number of $query->answer


    my $total = @answers;

    > - how to access the i'th answer; $query->answer[$i] does not work


    my $i_th = @answers[ $i];

    > - how to access a random answer


    my $rand_ans = @answers[ rand @answers];

    > thanks
    > gtoomey
    > --
    > use Net::DNS;
    > my $res = Net::DNS::Resolver->new;
    > my $query = $res->query("example.com", "NS");
    >
    > if ($query) {
    > foreach $rr (grep { $_->type eq 'NS' } $query->answer) {

    ^^^
    You're not running under strict! You've been around long enough to
    know better.

    > print $rr->nsdname, "\n";
    > }
    > }


    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 31, 2005
    #2
    1. Advertising

  3. Anno Siegel wrote:

    > Gregory Toomey <> wrote in comp.lang.perl.misc:
    >> I'm a bit confused about the Perl OO syntax. From my reading "foreach"
    >> acts as an iterator.

    >
    > It is a loop construct. An iterator is something that produces
    > values to iterate over, like <> or "each" in Perl.
    >
    >> Any somebody tell me in the standard example below:

    >
    > For all of these, do
    >
    > my @answers = $query->answer;
    >
    >
    >> - how to count the total number of $query->answer

    >
    > my $total = @answers;
    >
    >> - how to access the i'th answer; $query->answer[$i] does not work

    >
    > my $i_th = @answers[ $i];
    >
    >> - how to access a random answer

    >
    > my $rand_ans = @answers[ rand @answers];
    >
    >> thanks
    >> gtoomey
    >> --
    >> use Net::DNS;
    >> my $res = Net::DNS::Resolver->new;
    >> my $query = $res->query("example.com", "NS");
    >>
    >> if ($query) {
    >> foreach $rr (grep { $_->type eq 'NS' } $query->answer) {

    > ^^^
    > You're not running under strict! You've been around long enough to
    > know better.
    >
    >> print $rr->nsdname, "\n";
    >> }
    >> }

    >
    > Anno


    That code was from the NET::DNS module.

    I think I found the answer to my original question.
    The syntax $query->answer[$i] is wrong; ($query->answer)[$i] works even
    though it looks strange.

    gtoomey
    Gregory Toomey, Oct 31, 2005
    #3
  4. Gregory Toomey

    Anno Siegel Guest

    Gregory Toomey <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    >
    > > Gregory Toomey <> wrote in comp.lang.perl.misc:
    > >> I'm a bit confused about the Perl OO syntax. From my reading "foreach"
    > >> acts as an iterator.

    > >
    > > It is a loop construct. An iterator is something that produces
    > > values to iterate over, like <> or "each" in Perl.
    > >
    > >> Any somebody tell me in the standard example below:

    > >
    > > For all of these, do
    > >
    > > my @answers = $query->answer;
    > >
    > >
    > >> - how to count the total number of $query->answer

    > >
    > > my $total = @answers;
    > >
    > >> - how to access the i'th answer; $query->answer[$i] does not work

    > >
    > > my $i_th = @answers[ $i];
    > >
    > >> - how to access a random answer

    > >
    > > my $rand_ans = @answers[ rand @answers];
    > >
    > >> thanks
    > >> gtoomey
    > >> --
    > >> use Net::DNS;
    > >> my $res = Net::DNS::Resolver->new;
    > >> my $query = $res->query("example.com", "NS");
    > >>
    > >> if ($query) {
    > >> foreach $rr (grep { $_->type eq 'NS' } $query->answer) {

    > > ^^^
    > > You're not running under strict! You've been around long enough to
    > > know better.
    > >
    > >> print $rr->nsdname, "\n";
    > >> }
    > >> }

    > >
    > > Anno

    >
    > That code was from the NET::DNS module.


    So? You are responsible for the code you post, even if you take it from
    somewhere else.

    > I think I found the answer to my original question.
    > The syntax $query->answer[$i] is wrong; ($query->answer)[$i] works even
    > though it looks strange.


    It's a normal list slice, nothing strange about it. However, it
    will calculate the entire list of answers each time you want to
    extract a single element. Caching the list, as in my code above,
    avoids that.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 31, 2005
    #4
  5. Anno Siegel <-berlin.de> wrote:

    >> - how to access the i'th answer; $query->answer[$i] does not work

    >
    > my $i_th = @answers[ $i];

    ^
    ^

    my $i_th = $answers[ $i];


    >> - how to access a random answer

    >
    > my $rand_ans = @answers[ rand @answers];

    ^
    ^

    my $rand_ans = $answers[ rand @answers];


    :)

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 31, 2005
    #5
  6. -berlin.de (Anno Siegel) wrote in
    news:dk4s2t$e6r$-Berlin.DE:

    > my $i_th = @answers[ $i];


    Just a quick correction: @answers[$i] is an array slice; ITYM:

    my $i_th = $answers[ $i];

    ;-)

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 31, 2005
    #6
  7. Gregory Toomey

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > Anno Siegel <-berlin.de> wrote:
    >
    > >> - how to access the i'th answer; $query->answer[$i] does not work

    > >
    > > my $i_th = @answers[ $i];

    > ^
    > ^
    >
    > my $i_th = $answers[ $i];
    >
    >
    > >> - how to access a random answer

    > >
    > > my $rand_ans = @answers[ rand @answers];

    > ^
    > ^
    >
    > my $rand_ans = $answers[ rand @answers];


    Oh dear, yes!

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 31, 2005
    #7
  8. Gregory Toomey

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > -berlin.de (Anno Siegel) wrote in
    > news:dk4s2t$e6r$-Berlin.DE:
    >
    > > my $i_th = @answers[ $i];

    >
    > Just a quick correction: @answers[$i] is an array slice; ITYM:
    >
    > my $i_th = $answers[ $i];


    Oh, that too... Same mistake as with the random element you mercifully
    snipped. I was thinking Perl 6[1], that's my excuse.

    Anno

    [1] Though I don't understand how Larry can say Perl 6 is still going
    to be Perl when the variables don't change their sigils like a
    chameleon (ha!) its colors.
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 31, 2005
    #8
  9. Gregory Toomey

    Dr.Ruud Guest

    Anno Siegel:

    > I was thinking Perl 6


    I was thinking that you were thinking Perl 6.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Oct 31, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,765
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    475
    Kai-Uwe Bux
    May 8, 2005
  3. , India
    Replies:
    10
    Views:
    1,055
    James Kanze
    Aug 8, 2009
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    283
    PerlFAQ Server
    Jan 23, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    651
    PerlFAQ Server
    Feb 3, 2011
Loading...

Share This Page