Re: Idiom for array index that I'm foreach'ing over?
From: Brad Baxter (bmb_at_ginger.libs.uga.edu)
Date: 12/07/03
- Next message: A. Sinan Unur: "Re: How to write to drive A:\ from CGI Perl"
- Previous message: Guy: "Re: appending to file appends twice!?"
- In reply to: Anno Siegel: "Re: Idiom for array index that I'm foreach'ing over?"
- Next in thread: Iain Chalmers: "Re: Idiom for array index that I'm foreach'ing over?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 6 Dec 2003 21:32:05 -0500
On Fri, 5 Dec 2003, Anno Siegel wrote:
> Ben Morrow <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:
> > 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.
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
- Next message: A. Sinan Unur: "Re: How to write to drive A:\ from CGI Perl"
- Previous message: Guy: "Re: appending to file appends twice!?"
- In reply to: Anno Siegel: "Re: Idiom for array index that I'm foreach'ing over?"
- Next in thread: Iain Chalmers: "Re: Idiom for array index that I'm foreach'ing over?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|