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 ...
b R interrupt$VAR1 = [ [ 'a', 'W', 'interupt' ], [ 'b', 'R', 'interrupt' ], [ 'c', + + + 'W', + + + 'innterupt' + + + ], + + + [ + + + 'd', + + + 'W', + + + 'intterupt' + + + ] + + + ]; + + + ($answer,) = grep { $$_[0] eq 'b' } @$VAR1; + + + print "@$answer";
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 | |
|
Re^3: grep and dereferencing an array of arrays
by oakb (Scribe) on Sep 08, 2013 at 22:54 UTC |