On Monday 18 September 2006 18:43, Kenneth A Graves wrote:
> On Mon, 2006-09-18 at 08:28 -0700, Palit, Nilanjan wrote:
> > That works too (& I have used it as well), but I guess what I'm really
> > asking (hoping?) for is a Perl special variable, kinda like "$.", which
> > Perl auto-magically initializes & increments ...
>
> Perl6 will have a way of iterating over indices and values at the same
> time. I don't think anyone has back-ported it though.
Hi all.
Well, here is my attempt at writing such an iterator. Using it one gets the
following program:
<<<<<<<<<<<<<<
#!/usr/bin/perl
use strict;
use warnings;
use ArrayIter;
my @array = ("One Fish", "Two Fish", "Red Fish", "Blue Fish");
my $it = ArrayIter->new([EMAIL PROTECTED]);
while (my ($idx, $val) = $it->next())
{
print "array[$idx] = $val\n";
}
>>>>>>>>>>>>>>
To output:
<<<<<<<<<<<
array[0] = One Fish
array[1] = Two Fish
array[2] = Red Fish
array[3] = Blue Fish
>>>>>>>>>>>
And here's the module code:
<<<<<<<<<<<<<
package ArrayIter;
use strict;
use warnings;
use base 'Class::Accessor';
__PACKAGE__->mk_accessors(qw(
array_ref
idx
));
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_init(@_);
return $self;
}
sub _init
{
my $self = shift;
my $array_ref = shift;
$self->array_ref($array_ref);
$self->idx(0);
return 0;
}
sub next
{
my $self = shift;
if ($self->idx() == @{$self->array_ref()})
{
return;
}
else
{
my @ret = ($self->idx(), $self->array_ref->[$self->idx()]);
$self->idx($self->idx()+1);
# TODO : Do something meaningful for scalar context.
return @ret;
}
}
1;
>>>>>>>>>>>>>
Everything here is free for any use under the MIT X11 licence. (a BSD-style
Public Domain-like licence).
Of course I'm not sure it's actually any improvement, but who is John Galt?
Regards,
Shlomi Fish
BTW: the module and script worked perfectly after the first run, without a
single error. But there may still be some bugs.
> The various
> somewhat cumbersome ways to code it are your best approaches for now, if
> you really need the index.
>
> Note that you might be able to get away without the index, with a
> suitable rearrangement of your code:
>
> @newarray = map {complicated_function($_)} @array;
>
> --kag
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On
> > Behalf Of Dan Boger
> > Sent: Monday, September 18, 2006 10:39 AM
> > To: [email protected]
> > Subject: Re: [Boston.pm] Loop index in foreach?
> >
> > On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> > > In a foreach loop, is there a way to find out the loop index number?
> > > E.g.:
> > >
> > > foreach (@myarray)
> > > {
> > > ...
> > > push(@newarray[<??loopindex??>], <somevalue>);
> > > ...
> > > }
> > >
> > > Currently, I have to resort to the following:
> > > for (my $i= 0; $i <= $#myarray, $i++)
> > > { ... push(@newarray[$i], <somevalue>); ...}
> > >
> > > ... which is more wordy -- I was wondering if there's a more
> > > elegant/efficient (i.e. less code) to do this?
> >
> > Not sure if I'd call this more elegant, but perhaps
> >
> > my $index = 0;
> > foreach (@myarray) {
> > ...
> > push(@newarray[$index], <somevalue>);
> > ...
> >
> > $index++;
> > }
>
> _______________________________________________
> Boston-pm mailing list
> [email protected]
> http://mail.pm.org/mailman/listinfo/boston-pm
--
---------------------------------------------------------------------
Shlomi Fish [EMAIL PROTECTED]
Homepage: http://www.shlomifish.org/
Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm