in reply to RE: any-all
in thread any-all

Hmm.

I'll add 'local' declaration to it.

Thers little chance that someone will use sort there but who knows?

Replies are listed 'Best First'.
RE: RE: RE: any-all
by Adam (Vicar) on Sep 02, 2000 at 02:21 UTC
    Its worse then that... your tests do the same amount of abuse. Have you considered doing this with a closure?
    sub getAnyAll { my $code = shift; my $Any = sub { my @A = @{$_[0]}; # I like to dereference early, it makes my @B = @{$_[1]}; # the rest of the code more readable. while( @A and @B ) { return 1 if &$code( pop(@A), pop(@B) ) } return 0 }; my $All = sub { my @A = @{$_[0]}; my @B = @{$_[1]}; while( @A and @B ) { return 0 unless &$code( pop(@A), pop(@B) ) } return 1 }; return ( $Any, $All ); }
    that's untested, but gives you the idea. By the way... poping arrays passed by reference might not be such a good idea either...
      1. What is a closure?

      2. Writing $a is nicer (=shorter and more readable) than $_[0]

      3. I'd like to iterate through both arrays, something like
      "for $a=(@A), $b=(@B)" and thus the pop. Hopefuly perl just pops a pointer array to the original.

      The goal is a short and compact sub that will do the above.
        1. A closure is a sub-routine that generates and returns a subroutine. They are good for playing games with memory and scope. See my Base Conversion Utility for a good example.

        2. Writing $a may be nicer then $_[0] but its also more error prone and not properly scoped. $a and $b are also magic variables reserved for sort.

        3. Perl does not 'pop a pointer'. It dereferences the array and pops the last value off of it.

        Also, I think it is easier to read "while( @A and @B ){ dofunc( pop(@A), pop(@B) ) }" As this keeps you from poping @A until you know that @B still has values.