in reply to multiple greps at once-- possible?

To keep this problem from happening again, you might want to look at Quantum::Superpositions, specifically the any() and all() functions. List::Utils has some useful functions, as well. The reason I point you to them is that they will work better with grep than 'and' or '&&'.

Another solution to your direct problem is to put the "grep { } @arr" in parentheses.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: multiple greps at once-- possible?
by Abigail-II (Bishop) on Apr 08, 2004 at 16:07 UTC
    To keep this problem from happening again, you might want to look at Quantum::Superpositions, specifically the any() and all() functions.
    Eh, no, you don't. Quantum::Superpositions is extremely slow. There's no justification for using Quantum::Superpositions, just to save typing a pair of parenthesis. Here's a benchmark:
    #!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; use Quantum::Superpositions; our @data1 = "aa" .. "zz"; our @data2 = "AA" .. "ZZ"; my %queries = ( middle => [$data1 [@data1 / 2], $data2 [@data2 / 2]], first => [$data1 [0], $data2 [0]], not => ["feeble", "fnord"], ); foreach my $type (qw /middle first not/) { warn "Running '$type' test.\n"; our ($q1, $q2) = @{$queries {$type}}; our ($a_g, $a_q); cmpthese -1 => { grep => '$a_g = grep ({$q1 eq $_} @data1) && grep ({$q2 eq $_} @data2)', QS => '$a_q = $q1 eq any (@data1) && $q2 eq any (@data2)' +, }; die "Unequal" if $a_g xor $a_q; print "\n"; } __END__ Running 'middle' test. Rate QS grep QS 24.5/s -- -99% grep 2376/s 9588% -- Running 'first' test. Rate QS grep QS 24.1/s -- -99% grep 2400/s 9869% -- Running 'not' test. Rate QS grep QS 48.6/s -- -99% grep 5744/s 11719% --

    Abigail

Re: Re: multiple greps at once-- possible?
by hardburn (Abbot) on Apr 08, 2004 at 16:06 UTC

    Q::S is not a small module. While any and all can be implemented with a lot less code, the 'Quantum' part of is what makes Q::S so large. IMHO, using it almost anywhere except as a toy is going to be massive overkill.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Re: multiple greps at once-- possible?
by mpeppler (Vicar) on Apr 08, 2004 at 16:04 UTC
    Another solution to your direct problem is to put the "grep { } @arr" in parentheses.
    Indeed... personally I've never been one to favor the "new" style of only using parens when only absolutely necessary. Precendence rules are tricky (i.e. the && vs. and). By using parens I can at least explicitly tell the compiler what I want (and it helps visually as well, IMHO). I find
    open(IN, "foo") || die "...";
    much easier to read than
    open IN, "foo" or die "...";
    Michael