Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > OO Perl, iterators

Reply
Thread Tools

OO Perl, iterators

 
 
Gregory Toomey
Guest
Posts: n/a
 
      10-31-2005
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:NS;
my $res = Net:NS::Resolver->new;
my $query = $res->query("example.com", "NS");

if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
print $rr->nsdname, "\n";
}
}
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-31-2005
Gregory Toomey <(E-Mail Removed)> 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:NS;
> my $res = Net:NS::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.
 
Reply With Quote
 
 
 
 
Gregory Toomey
Guest
Posts: n/a
 
      10-31-2005
Anno Siegel wrote:

> Gregory Toomey <(E-Mail Removed)> 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:NS;
>> my $res = Net:NS::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:NS 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-31-2005
Gregory Toomey <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> > Gregory Toomey <(E-Mail Removed)> 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:NS;
> >> my $res = Net:NS::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:NS 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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-31-2005
Anno Siegel <(E-Mail Removed)-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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-31-2005
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in
news:dk4s2t$e6r$(E-Mail Removed)-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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-31-2005
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel <(E-Mail Removed)-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.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-31-2005
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed)-berlin.de (Anno Siegel) wrote in
> news:dk4s2t$e6r$(E-Mail Removed)-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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-31-2005
Anno Siegel:

> I was thinking Perl 6


I was thinking that you were thinking Perl 6.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
Idempotent ODBMS iterators Paul Chapman Java 0 02-16-2005 01:51 PM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM
Any interest in lightweight coroutines in Java ala C# 2.0 iterators? Ken Sprague Java 4 10-28-2003 08:03 PM



Advertisments