D
Dr.Ruud
Inspired by List::MoreUtils, I came up with the following.
Please comment on the name of the sub, and on any problems
you see, and on what not.
#!/usr/bin/perl
use strict;
use warnings;
# pairwise_firstidx BLOCK ARRAY1 ARRAY2
Please comment on the name of the sub, and on any problems
you see, and on what not.
#!/usr/bin/perl
use strict;
use warnings;
# pairwise_firstidx BLOCK ARRAY1 ARRAY2
# returns the first index for which the BLOCK returns a defined value,
# optionally only for a list of index values
sub pairwise_do (&\@\@;@)
{
use vars qw/$a $b/;
my $op = shift; # the BLOCK
my ($caller_a, $caller_b) = do
{ my $pkg = caller();
no strict 'refs';
\*{$pkg.'::a'}, \*{$pkg.'::b'};
};
my $retval;
local (*$caller_a, *$caller_b);
for ( $_[2]
?
@_[2..$#_]
:
$[ .. $#{$_[0]} >= $#{$_[1]}
?
$#{$_[0]}
:
$#{$_[1]}
) {
(*$caller_a, *$caller_b) = \($_[0][$_], $_[1][$_]);
defined ($retval = $op->()) and return $retval;
}
return;
}
my @x = (0..6, 70..99);
my @y = (0..6, 80..99);
# find the index of the first non-equal value for
# two arrays, and within a specific index range:
print pairwise_do { $a != $b ? $_ : undef } @x, @y, 2..9;
__END__