Re: OO Perl, iterators
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 31 Oct 2005 10:36:13 GMT
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.
.
- Follow-Ups:
- Re: OO Perl, iterators
- From: A. Sinan Unur
- Re: OO Perl, iterators
- From: Gregory Toomey
- Re: OO Perl, iterators
- References:
- OO Perl, iterators
- From: Gregory Toomey
- OO Perl, iterators
- Prev by Date: Re: RTF and UTF-8 files in Perl
- Next by Date: 'Must occur' -> 'Match' (was: Re: Question about perlreref - are {n} and {n}? different?)
- Previous by thread: OO Perl, iterators
- Next by thread: Re: OO Perl, iterators
- Index(es):
Relevant Pages
|