in reply to Re: grep and dereferencing an array of arrays
in thread grep and dereferencing an array of arrays

You're right about the mistake, except that what grep returns is already a hashref so by adding [ ] around it, you make an array of array

You can just provide a list context to the expression with ($usefulans,) (the comma is optional, but I put it there to show that the parenthesis is there to force list context), which will put the first result into $usefulans and drop the others. But since there should be only one match ...

$VAR1 = [ [ 'a', 'W', 'interupt' ], [ 'b', 'R', 'interrupt' ], [ 'c', + + + 'W', + + + 'innterupt' + + + ], + + + [ + + + 'd', + + + 'W', + + + 'intterupt' + + + ] + + + ]; + + + ($answer,) = grep { $$_[0] eq 'b' } @$VAR1; + + + print "@$answer";
b R interrupt

But oakb, you should consider a hash (with answer id as the key). You could easily turn your array into one with : %hash = map { shift @$_ => $_ } @$qablock Then you'd just have to do $hash{$givenAnswer} to get ['w', 'interupt']

Replies are listed 'Best First'.
Re^3: grep and dereferencing an array of arrays
by McA (Priest) on Sep 06, 2013 at 21:03 UTC

    A ++ for your hash advice.

    McA

Re^3: grep and dereferencing an array of arrays
by oakb (Scribe) on Sep 08, 2013 at 22:54 UTC
    Using a hash is a great idea, one that I contemplated earlier but discarded because other parts of the program rely on the data being maintained in an ordered list -- which arrays provide but hashes do not.

    Honestly, I'm still trying to get my head around why the grep returns its data in a list context while the foreach does not. I expected the two routines to be equivalent....