Idiom for array index that I'm foreach'ing over?

B

Brad Baxter

{{ ^^
my $i=0;
sub rewind{$i=0}
sub eAch(\@\@){$i>$#{$_[0]}&&$i>$#{$_[1]}?():($_[0][$i],$_[1][$i ++])}
}}
^^

Why double braces?!? Ah! Nice JAPH btw...

Thanks. :) The double braces are just a nod to Anno's post on Nov 22,
re: top level blocks and indentation.

Regards,

Brad
 
B

Brad Baxter

Brad Baxter said:
Here's one non-destructive version:

my %h;
@h{@xx}=@yy;
for ( map [ $_, $h{$_}], @xx ) {
my ( $x, $y) = @$_;
print "x: $x, y: $y\n";
}

No. That will generally fail when the elements in @xx aren't unique.

Too true. I was telling myself, "but that requires an extra copy of
everything", and I missed the real reason it's wrong.

Here's another. :)

{{
my $i=0;
sub rewind{$i=0}
sub eAch(\@\@){$i>$#{$_[0]}&&$i>$#{$_[1]}?():($_[0][$i],$_[1][$i ++])}
}}

Oh, the double-brace convention. I'm feeling flattered :)

Just wanted to show I wasn't sleeping in class. :) An aspect of that
convention that appeals particularly is, I think, that it somewhat
emulates file-scoping traits in your module example. That is, the
lexicals in each package would not be seen in the other packages. Yes, a
single brace would work, too, but being double is an instant clue.

Another variation of the each-for-lists theme, with a nice use of
prototypes. If we had a prototype for "arbitrarily many of \@", this
could be generalized to a step-by-step transposition routine for any
number of parallel arrays.

Yes. I'm imagining tricks using eval, overload, Hairy? But inspiration
is low.

Regards,

Brad
 
A

Anno Siegel

Ben Morrow said:
BEGIN {{
my $i = -1;
sub rewind { $i = -1 }

sub them (;\@\@\@\@\@) {
$i++; # increment early, so we can return a valid index
return if $i >= @{ $_[ 0]};
( $i, map $_->[ $i], @_);
}
}}

while ( my ( $i, $x, $y, $t) = them( @xx, @yy, @tt) ) {
print "i: $i, x: $x, y: $y, t: $t\n";
}

Of course this is utterly fragile when applied to more than one set
of arrays, but for the given purpose it would do.

To remove some of the fragility (untested):

Runs here.
BEGIN {{
my %i;

sub rewind (;\@\@\@\@\@) { $i{join "", @_} = -1 }

sub them (;\@\@\@\@\@) {
my $set = join "", @_;
$i{$set} = -1 unless defined $i{$set}; # $i{$set} //= -1;
$i{$set}++;
rewind(@_), return if grep { $i{$set} >= @$_ } @_;
# I'm not sure what we want here... 'if @_ == grep ...' may be better.

So far, I have taken the first array to be the measure of all. Oh, so the
prototype should be (\@;\@...), a bug. Yours take the maximum or the
minimum length of all arrays offered, which is more thorough. There may
be other useful strategies.
( $i{$set}, map $_->[$i{$set}], @_);
}
}}

The code can be simplified a bit with re-introduction of a plain scalar $i.
In particular, we can lose the pesky initialization to -1.

my %i;

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub them (;\@\@\@\@\@) {
my $i = $i{ join "", @_} ++; # increment for next time, use current
rewind(@_), return if grep { $i >= @$_ } @_;
( $i, map $_->[$i], @_);
}

I think only a purist would complain about the use of $i and %i here.

Anno
 
M

Michele Dondi

Michele Dondi said:
$i ++; # hi Abigail
^ [snip]
I *think* it is because of that space. And I can't believe it: it...
it really works!

What's so unbelievable about it?

Hmmm... maybe that I didn't think it was possible?!?


Well, you can do funny things in Perl, particularly with whitespaces:

ethan@ethan:~$ perl
$
# comment
bla


= 5
;

print $bla;
__END__
5

I didn't expect this to be possible either. Funny!

The trick doesn't always work, anyway:

# perl -le 'print $i ++ for 1..3'
syntax error at -e line 1, near "++ for "
Execution of -e aborted due to compilation errors.

# perl -le 'print($i ++) for 1..3'
syntax error at -e line 1, near "++ for "
Execution of -e aborted due to compilation errors.

# perl -le 'print +($i ++) for 1..3'
0
1
2

# perl -le 'print do{$i ++} for 1..3'
0
1
2

Could you please be so kind and explain me what perl understands in
the respective cases? In particular I am not puzzled by the fact that
the first one fails and the last one succeeds, but I can't understand
why the second one does not.

As a side (minor) note, by the examples above one sees that even if
the postfix form of the auto increment operator should *increment* the
variable after returning its value, in practice it *modifies* its
value before returning it:

# perl -le "print $i; print $i++"

0

I checked perldoc perlop and read the magic bit about the
auto-increment operator with strings, but I don't think it applies
here. Also, it doesn't treat explicitly of the case of undefined
variables.


Michele
 
B

Ben Morrow

Michele Dondi said:
Hmmm... maybe that I didn't think it was possible?!?

As a general rule, Perl *completely* ignores whitespace. The only
exception I know of is when you have two sequences of \w next to other
which should be interpreted as separate identifiers: ws is required
then.
I didn't expect this to be possible either. Funny!

Comments are whitespace.
The trick doesn't always work, anyway:

# perl -le 'print $i ++ for 1..3'
syntax error at -e line 1, near "++ for "
Execution of -e aborted due to compilation errors.

perl -le'print STDOUT $i ++ for 1..3'
# perl -le 'print($i ++) for 1..3'
syntax error at -e line 1, near "++ for "
Execution of -e aborted due to compilation errors.

perl -le'print (STDOUT $i ++) for 1..3'
perl -le'print STDOUT ($i ++) for 1..3'

The problem here is that the rule for parsing an indirect object is
very greedy: as soon as it has something that could be interpreted as
one, it grabs it. So 'print $i ++' is interpreted trying to print '++'
to the filehandle '$i': the '++' is then a syntax error. Adding the
brackets makes no difference, *even* if they have whitespace before
them (I keep getting bitten by this :).
# perl -le 'print +($i ++) for 1..3'

perl -le'print +$i ++ for 1..3'
# perl -le 'print do{$i ++} for 1..3'

An indirect object cannot start with '+', or with 'do', so there's no
problem here. See perlobj/"Indirect Object Syntax".
Could you please be so kind and explain me what perl understands in
the respective cases? In particular I am not puzzled by the fact that
the first one fails and the last one succeeds, but I can't understand
why the second one does not.

I was initially puzzled as to why *any* of them failed...
As a side (minor) note, by the examples above one sees that even if
the postfix form of the auto increment operator should *increment* the
variable after returning its value, in practice it *modifies* its
value before returning it:

# perl -le "print $i; print $i++"

0

Also, it doesn't treat explicitly of the case of undefined
variables.

Err...

| "undef" is always treated as numeric, and in particular is changed
| to 0 before incrementing (so that a post-increment of an undef value
| will return 0 rather than "undef").

This is from perlop in 5.8.2.

Ben
 
T

Tassilo v. Parseval

Also sprach Ben Morrow:
As a general rule, Perl *completely* ignores whitespace. The only
exception I know of is when you have two sequences of \w next to other
which should be interpreted as separate identifiers: ws is required
then.

It's a little more complicated in fact. Here's one that Abigail
simply adores:

ethan@ethan:~$ perl -lw
print(1+2)*3;
Useless use of multiplication (*) in void context at - line 1.
3
ethan@ethan:~$ perl -lw
print (1+2)*3;
print (...) interpreted as function at - line 1.
Useless use of multiplication (*) in void context at - line 1.
3

The additional whitespace in the second example will trigger an
additional warning.

Tassilo
 
D

Darin McBride

Tim said:
perl data structures use references which are not the same as
objects. to do a 1-1 mapping of two arrays is simple without
indexes. depending on where the data cam from you can just create a
array of arrays ( [ x, y ] coordinates ) or even add the time value as
the third array element. or you could do a simple hash like:

{ x => 1.2,
y => 3.4,
time => 12335,
}

and have an array or hash of those (not sure what the hash key would
be).

All those are workable but IMHO a bit ugly compared to creating a

As I once heard, you can write Fortran in any language. The point is:
don't. The hash above is exactly what one should do in perl. If you
were in C, I would say put everything in a struct, and have an array of
those. In Perl, put everything in a hash, and have an array of refs to
those.
"point" object that has x,y, and time methods. Your last suggestion
comes close but I don't like all those curly brackets (again, my
personal taste).

Sorry - if you don't like braces, perl is not for you. Perl is all
about simple, short, if someone obtuse, ways of saying things. Most of
the punctuation has meaning, and if you don't like it, you'll need to
find another language. Perhaps REXX may be more to your liking.
But it's where my mind goes for any quick-and-dirty program.

In other words, you still are thinking Fortran rather than thinking
perl. To be honest, very few languages would encourage that type of
programming, so if you want to spread out past Fortran, I would
encourage you to embrace it.
 
B

Brad Baxter

The code can be simplified a bit with re-introduction of a plain scalar $i.
In particular, we can lose the pesky initialization to -1.

my %i;

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub them (;\@\@\@\@\@) {
my $i = $i{ join "", @_} ++; # increment for next time, use current
rewind(@_), return if grep { $i >= @$_ } @_;
( $i, map $_->[$i], @_);
}

You guys are something else. It only just occurred to me that you've
provided for the following to work:

my @aa = qw( a b c d e f );
my @nn = qw( 1 2 3 4 );
my @xx = qw( x y z );

print them( @aa, @nn, @xx ), "\n";
print these( \@aa, \@nn, \@xx ), "\n";
print them( @aa, @nn, @xx ), "\n";

sub these {
my( $q, $w, $e ) = @_;
them( @$q, @$w, @$e );
}

Nice. I wonder if it would be useful to have a query for the position.
(I had to fiddle with the order of things ...)

{{
my %i;

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub saw (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }

sub them (;\@\@\@\@\@) {
my $s = join "", @_;
my $i = $i{ $s }||0;
rewind(@_), return if grep { $i >= @$_ } @_;
$i{ $s } ++;
( $i, map $_->[$i], @_);
}
}}

my @aa = qw( a b c d e f );
my @nn = qw( 1 2 3 4 );
my @xx = qw( x y z );

print "saw: ", saw( @aa, @nn, @xx ), "\n";

while( my ( $i, $a, $n, $x ) = them( @aa, @nn, @xx ) ) {
print "$i, $a, $n, $x\n";
print "saw: ", saw( @aa, @nn, @xx ), "\n";
}

print "saw: ", saw( @aa, @nn, @xx ), "\n";
_____
saw: -1
0, a, 1, x
saw: 0
1, b, 2, y
saw: 1
2, c, 3, z
saw: 2
saw: 2

Regards,

Brad
 
B

Brad Baxter

{{
my %i;

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub saw (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }

sub them (;\@\@\@\@\@) {
my $s = join "", @_;
my $i = $i{ $s }||0;
rewind(@_), return if grep { $i >= @$_ } @_;
$i{ $s } ++;
( $i, map $_->[$i], @_);
}
}}


Pardon the autofollowup. That rewind(@_) isn't quite right. Here's a
reason for using '&'.

sub them (;\@\@\@\@\@) {
my $s = join "", @_;
my $i = $i{ $s }||0;
&rewind, return if grep { $i >= @$_ } @_;
$i{ $s } ++;
( $i, map $_->[$i], @_);
}

Regards,

Brad
 
B

Brad Baxter

So far, I have taken the first array to be the measure of all. Oh, so the
prototype should be (\@;\@...), a bug. Yours take the maximum or the
minimum length of all arrays offered, which is more thorough. There may
be other useful strategies.

Regarding other strategies, I was thinking of an option to "go out of
bounds", i.e., to take the maximum length in the set of arrays.
Assigning to the "goob" routine both chooses this option and sets the
value to return when you're out of bounds., e.g.

goob( @aa, @bb ) = ''; # or perhaps 0 or even undef

Do you see a better way to do that?


my %i;
my %goob; # parallel hashes, no less

sub goob (;\@\@\@\@\@) : lvalue { $goob{ join "", @_ } }

sub start (;\@\@\@\@\@) : lvalue { $i{ join "", @_} }

sub saw (;\@\@\@\@\@) { ($i{ join "", @_}||0) - 1 }

sub rewind (;\@\@\@\@\@) { delete $i{ join "", @_} }

sub them (;\@\@\@\@\@) {
my $set = join "", @_;
my $i = $i{ $set } ++; # increment for next time, use current
if( exists $goob{ $set } ) {
&rewind, return unless grep { $i < @$_ } @_;
return ( $i, map { $i < @$_ ? $_->[$i] : $goob{ $set } } @_);
}
&rewind, return if grep { $i >= @$_ } @_;
( $i, map $_->[$i], @_);
}


Regards,

Brad
 
I

Iain Chalmers

I think only a purist would complain about the use of $i and %i here.

But remember, you're helping out a _Fortran_ guy here... $i as an array
index, sure - but as a *hash* name???

:)

big
 
M

Michele Dondi

Err...

| "undef" is always treated as numeric, and in particular is changed
| to 0 before incrementing (so that a post-increment of an undef value
| will return 0 rather than "undef").

This is from perlop in 5.8.2.

Still 5.8.0 here, and unless I've gone completely dumb, that bit is
not in perlop.


Michele
 
M

Michele Dondi

Well, yes, of course. This is after all Perl5, which like every other
main stream language I know about allows optional whitespace between
tokens. The whitespace isn't significant.

Unlike Perl6, which just throws out 50 years of programming language
design out of the window, passes Python on the wrong side and makes
whitespace significant in new painful and revolting ways.

Just out of curiosity: is this definitive or are there chances that
such "features" may be removed/changed before before Perl6 is actually
released in productive form?


Michele
 
B

Brad Baxter

[This stems somewhat from discussions in the thread:
Idiom for array index that I'm foreach'ing over?]

I'm considering making a new module with the tentative name Array::Each.

A draft is available here:

http://www.vitabrevis.ws/perl/modules/Array/Each.pm
http://www.vitabrevis.ws/perl/modules/Array/Each.pod.html

It's incomplete, but I want to discuss the idea before proceeding further.

1. Is Array::Each acceptable? Perhaps Array::parallel, or
Array::Iteration?

2. Are the subroutine names acceptable? In particular, I expect negative
reactions to each(), since it would clobber each( %hash ), so alternative
suggestions are welcome.

3. Is this a good approach? Are all those options needed? Does it matter
that it doesn't return lvalues like foreach ( @array )?

4. Should someone else do this/Has someone else done this? I did study
the CPAN module list and didn't find a close match. That I missed one
would not surprise me.

5. I imagine sometime adding an OO interface along these lines:

my $set = Array::Each->new( @x, @y );
my( $i, $x, $y ) = $set->each();

Among other things, this should allow iterating over the same set of
arrays using different iterators. Should I bother?


All feedback is appreciated.

Thanks,

Brad
 
B

Bryan Castillo

Brad Baxter said:
[This stems somewhat from discussions in the thread:
Idiom for array index that I'm foreach'ing over?]

I'm considering making a new module with the tentative name Array::Each.

A draft is available here:

http://www.vitabrevis.ws/perl/modules/Array/Each.pm
http://www.vitabrevis.ws/perl/modules/Array/Each.pod.html

It's incomplete, but I want to discuss the idea before proceeding further.

1. Is Array::Each acceptable? Perhaps Array::parallel, or
Array::Iteration?

2. Are the subroutine names acceptable? In particular, I expect negative
reactions to each(), since it would clobber each( %hash ), so alternative
suggestions are welcome.

3. Is this a good approach? Are all those options needed? Does it matter
that it doesn't return lvalues like foreach ( @array )?

4. Should someone else do this/Has someone else done this? I did study
the CPAN module list and didn't find a close match. That I missed one
would not surprise me.

5. I imagine sometime adding an OO interface along these lines:

my $set = Array::Each->new( @x, @y );
my( $i, $x, $y ) = $set->each();

Among other things, this should allow iterating over the same set of
arrays using different iterators. Should I bother?


All feedback is appreciated.

Thanks,

Brad

The code below ............

use strict;
use warnings;
use Array::Each qw/them rewind/;
$|=1;
while (1) {
my @array = map{rand}(1..10);
while (my ($k,$v) = them(@array)) {
print "[$k:$v]";
last if ($k == 0);
}
print "\nnext -> ";
### rewind(@array); # would fix it
}

produces..............

next -> [1:0][2:8]
next -> [0:4]
next -> [1:4][2:0]
next -> [0:1]
next -> [1:0][2:3]
next -> [0:0]
next -> [1:9][2:9]
next -> [0:1]
next -> [1:6][2:4]
next -> [0:1]
next -> [1:0][2:7]
next -> [0:0]


I realize that this occurs, because the variable has the same
reference
even though it has completely changed. You might want to note
something
about breaking out of iteration and how you should call rewind if you
are
going to be using the same variable in a loop.

Anyway, I could see how someone might write code that leaves entries
in your %set variable which are never deleted. This might be a
problem for a daemon.


Perhaps, some type of syntax as shown below would work.....
Without keeping a global hash.......
Of course it would have to be modified to iterate over multiple
arrays.

(I like your syntax better, I just don't like the way context is
maintained.
Although I don't see any better way to do it, while keeping your
syntax.)

use strict;
use warnings;

sub on_each(&@) {
my $code = shift;
for (my $i=0; $i<=$#_; $i++) {
$code->($i, $_[$i]);
}
}

on_each {
print "Index = $_[0] and Value = $_[1]\n";
} (1..10);


I really don't like the idea of the function being named each, but
that is just my opinion.
 
A

Anno Siegel

Brad Baxter said:
[This stems somewhat from discussions in the thread:
Idiom for array index that I'm foreach'ing over?]

I'm considering making a new module with the tentative name Array::Each.

A draft is available here:

http://www.vitabrevis.ws/perl/modules/Array/Each.pm
http://www.vitabrevis.ws/perl/modules/Array/Each.pod.html

It's incomplete, but I want to discuss the idea before proceeding further.

1. Is Array::Each acceptable? Perhaps Array::parallel, or
Array::Iteration?

2. Are the subroutine names acceptable? In particular, I expect negative
reactions to each(), since it would clobber each( %hash ), so alternative
suggestions are welcome.

3. Is this a good approach? Are all those options needed? Does it matter
that it doesn't return lvalues like foreach ( @array )?

4. Should someone else do this/Has someone else done this? I did study
the CPAN module list and didn't find a close match. That I missed one
would not surprise me.

5. I imagine sometime adding an OO interface along these lines:

my $set = Array::Each->new( @x, @y );
my( $i, $x, $y ) = $set->each();

By all means, go for an OO approach. This stuff has been shouting
"object" from the moment we introduced the %i hash with its keys to
distinguish different iterators.

The user will have to create an iterator object (instead of "spontaneously"
saying "( $i, $x, $y) = each( @x, @y)"), but it will be much clearer
what's happening.
Among other things, this should allow iterating over the same set of
arrays using different iterators. Should I bother?

That's one of the advantages of explicit iterators.
All feedback is appreciated.

Make each() return the index last (after the array elements) instead
of first. That way the first n values correspond to the n arrays, and
the index is easy to ignore when it isn't needed.

Anno
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Just out of curiosity: is this definitive or are there chances that
such "features" may be removed/changed before before Perl6 is actually
released in productive form?

Alas, very slim. :-( I have this nagging feeling that version 6 is
going to be the worst thing that ever happened to Perl.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/1w+jY96i4h5M0egRAml7AJ9tDxJsIzrhWlGO+pTdrzI3t90D7wCgpdp3
RRhonHoRGuZerY3Gue1XFzA=
=jgan
-----END PGP SIGNATURE-----
 
B

Bryan Castillo

Brad Baxter said:
[This stems somewhat from discussions in the thread:
Idiom for array index that I'm foreach'ing over?]

I'm considering making a new module with the tentative name Array::Each.

A draft is available here:

http://www.vitabrevis.ws/perl/modules/Array/Each.pm
http://www.vitabrevis.ws/perl/modules/Array/Each.pod.html

It's incomplete, but I want to discuss the idea before proceeding further.

1. Is Array::Each acceptable? Perhaps Array::parallel, or
Array::Iteration?

2. Are the subroutine names acceptable? In particular, I expect negative
reactions to each(), since it would clobber each( %hash ), so alternative
suggestions are welcome.

3. Is this a good approach? Are all those options needed? Does it matter
that it doesn't return lvalues like foreach ( @array )?

4. Should someone else do this/Has someone else done this? I did study
the CPAN module list and didn't find a close match. That I missed one
would not surprise me.

5. I imagine sometime adding an OO interface along these lines:

my $set = Array::Each->new( @x, @y );
my( $i, $x, $y ) = $set->each();

By all means, go for an OO approach. This stuff has been shouting
"object" from the moment we introduced the %i hash with its keys to
distinguish different iterators.

Isn't there some type of Iterator package for perl? Why stop at arrays?
If there would be an OO approach, why not have iterators for many things.


Here is a simplified example of what Im thinking about.
It is using closures instead of full objects (for brevity);


package Iterator;
use strict;
use warnings;

# iterator over multiple arrays
sub arrays(;\@\@\@\@\@) {
my $arrays = [@_];
my $i = -1;
my $mi = -1;
foreach(@_) { $mi = $#{$_} if ($#{$_} > $mi) }
return sub {
return () if ($i >= $mi);
return (++$i, map{$_->[$i]} @{$arrays});
};
}

# iterate over multiple hashes
sub hashes(;\%\%\%\%\%) {
my $hashes = [@_];
my %keys;
map{$keys{$_}=1} keys %{$_} foreach(@_);
my @keys = keys %keys;
my $i = -1;
return sub {
return () if ($i >= $#keys);
return ($keys[++$i], map{$_->{$keys[$i]}} @{$hashes});
};
}

# iterate over 2d array
sub array2d {
my ($x,$y) = (-1,0);
my $array = ($#_ == 0 and UNIVERSAL::isa($_[0], 'ARRAY')) ? $_[0] : [@_];
my $my = $#{$array};
my $mx = ($my >= 0) ? $#{$array->[0]} : -1;
return sub {
if (++$x > $mx) { $y++; $x=0 }
return () if ($y > $my);
return ($y,$x,$array->[$y][$x]);
};
}

# Find and load dynamic iterator
sub find_iterator {
my $imod = shift;
print "imod = $imod\n";
my $imod_file = "Iterator/$imod";
$imod_file =~ s/::/\//g;
$imod_file .= ".pm";
require $imod_file;
no strict 'refs';
my $get_iter = \&{*{"Iterator::${imod}::iterator"}};
warn "not iterator found for $imod\n" and return unless($get_iter);
return $get_iter->(@_);
}

package main;
use strict;
use warnings;
no warnings 'uninitialized';

my @a = qw/ 1 2 3 4/;
my @b;
my @c = qw/6 7 8 9/;

my $iter_a = Iterator::arrays(@a,@b,@c);
while (my ($i,$v1,$v2,$v3) = $iter_a->()) {
print "[$i] ($v1,$v2,$v3)\n";
}

print "-" x 40, "\n";

my %a = (a=>1,b=>2,c=>3);
my %b = (z=>99);
my %c = (xx=>'xx',hash=>\%a);

my $iter_b = Iterator::hashes(%a, %b, %c);
while (my ($k,$v1,$v2,$v3) = $iter_b->()) {
print "[$k] ($v1,$v2,$v3)\n";
}

print "-" x 40, "\n";

my $array = [
[1,2,3],
[4,5,6],
[7,8,9]
];

my $iter_c = Iterator::array2d($array);
while (my ($y,$x,$value) = $iter_c->()) {
print "($y,$x) = ($value)\n";
}

print "-" x 40, "\n";

my $iter_d = Iterator::find_iterator('files', "*.pl");
while (my ($i, $file) = $iter_d->()) {
print "[$i] = $file\n";
}





# Iterator/files.pm
package Iterator::files;
use strict;
use warnings;

sub iterator {
my @files = sort glob(shift);
my $i = -1;
return sub {
return () if (++$i > $#files);
return ($i, $files[$i]);
};
}

return 1;
 
M

Michele Dondi

{} >Unlike Perl6, which just throws out 50 years of programming language
{} >design out of the window, passes Python on the wrong side and makes
{} >whitespace significant in new painful and revolting ways.
{}
{} Just out of curiosity: is this definitive or are there chances that
{} such "features" may be removed/changed before before Perl6 is actually
{} released in productive form?

Nothing is definitive, but Larry and Damian seem to be convinced that
the gain of saving 2 characters on an if() are worth sacrificing the
non-significance of whitespace, that I say the chances are small.

It would be nice if this behaviour could be triggered somehow, say by
means of yet another cmd line switch or a pragma...

But shouldn't Perl6 be the "community rewrite" (IIRC) of Perl? What
does the community think here?!?


Alas, very slim. :-( I have this nagging feeling that version 6 is
going to be the worst thing that ever happened to Perl.

Well I know next to nothing about Perl6, but as a general disposition
I feel inclined to anticipate with enthusiasm any kind of innovation,
not to fear it.

Now, what I came to know in this thread is certainly something that I
completely dislike, agreeing with you both (FWIW MHO!). But I'll wait
to see the living creature...


BTW: what would be the best resource for discussing own
ideas/questions about feaures of (any future major release of Perl,
but then most definitely) Perl6? Please not that I'm talking about
(possibly) naive or generic issues, so too technical a
forum/board/whatever may not be well suited for them...


Michele
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[This stems somewhat from discussions in the thread:
Idiom for array index that I'm foreach'ing over?]

I'm considering making a new module with the tentative name
Array::Each.

A draft is available here:

http://www.vitabrevis.ws/perl/modules/Array/Each.pm
http://www.vitabrevis.ws/perl/modules/Array/Each.pod.html

It's incomplete, but I want to discuss the idea before proceeding
further.

1. Is Array::Each acceptable? Perhaps Array::parallel, or
Array::Iteration?

2. Are the subroutine names acceptable? In particular, I expect
negative reactions to each(), since it would clobber each( %hash ), so
alternative suggestions are welcome.

3. Is this a good approach? Are all those options needed? Does it
matter that it doesn't return lvalues like foreach ( @array )?

4. Should someone else do this/Has someone else done this? I did
study the CPAN module list and didn't find a close match. That I
missed one would not surprise me.

5. I imagine sometime adding an OO interface along these lines:

my $set = Array::Each->new( @x, @y );
my( $i, $x, $y ) = $set->each();

Among other things, this should allow iterating over the same set of
arrays using different iterators. Should I bother?

I think an OO interface is a good idea. If you do that, don't return
the iteration value with the array values. Use a separate method for
that.

Perhaps something like this:

$set = Array::Each->new (@x, @y);
while (my ($x, $y) = $set->each())
{
print "Iteration number ", $set->index(), "\n";
print " x = $x; y = $y\n";
print " last iteration!\n" if $set->exhausted();
}
$set->rewind();

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/19BsY96i4h5M0egRAmQeAKDXldiTn7DoAUwg99VLtKZDdEvfMgCgie64
TKeuP9sACsC0O/+Z2PhhAz8=
=z5kt
-----END PGP SIGNATURE-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top