Re: OO Perl, iterators



Gregory Toomey <nobody@xxxxxxxxxxx> 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.
.



Relevant Pages

  • Re: comparison of objects
    ... the standard function is being used versus your redefinition. ... User's interface would give means to iteration, ... ** User and implementor interface: ... An iterable is an object for which an iterator can be defined. ...
    (comp.lang.lisp)
  • Re: Erase in a map
    ... According to the standard, map::erase does return nothing, whereas, say, ... returning an iterator as a conforming extension, ... vector::erase on the other hand invalidates ... erase() method does return an iterator). ...
    (microsoft.public.vc.stl)
  • Re: Erase in a map
    ... erasereturns an iterator of the element after the erased ones. ... According to my pre-release copy of the standard, ... containers that model the sequence requirement. ...
    (microsoft.public.vc.stl)
  • Re: Problem with pointers and iterators
    ... In VC6 this worked because vector::iterator was a typedef for T*. ... Both forms satisfy the requirements of the standard. ... Well, you have an iterator that refers to an object, and you need the address of that object. ... get a reference from the iterator, ...
    (comp.lang.c)
  • Re: Explain the magic? Counting lines in a file
    ... > you in to the standard name space, what I want to know is what namespace ... Iterator types are special classes that usually belong ... point to a specific element of a container instead of to a specific memory ... Such iterators are used in most standard algorithms, ...
    (comp.lang.cpp)