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

From: Brad Baxter (bmb_at_ginger.libs.uga.edu)
Date: 12/07/03


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



Relevant Pages

  • Choose two numbers
    ... code finds the largest number in the first row of the first array ... By writing these two numbers from each row of the arrays ... Dim lastrow As Long ... Wend ...
    (microsoft.public.excel.programming)
  • Re: SQLBulkOperations question
    ... The arrays are not of the same length. ... T> gives an error saying that duplicate values are being written though ... T> If the table does not have a primary key things go ok. ... T> BulkOps without the totla data being a multiple of the first array? ...
    (microsoft.public.data.odbc)
  • SQLBulkOperations question
    ... In trying to write to a table WITH a primary key, ... The arrays are not of the same length. ... The error seems to be given on the SECOND row of the array2, ... without the totla data being a multiple of the first array? ...
    (microsoft.public.data.odbc)
  • Question on Use of BulkOperations
    ... In trying to write to a table WITH a primary key, ... The arrays are not of the same length. ... The error seems to be given on the SECOND row of the array2, ... without the total data being a multiple of the first array? ...
    (microsoft.public.sqlserver.odbc)