Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: grep and dereferencing an array of arrays

by McA (Priest)
on Sep 06, 2013 at 20:34 UTC ( #1052763=note: print w/ replies, xml ) Need Help??


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

Ok, that puts some light on your problem. This may be your solution:

#!/usr/bin/env perl use strict; use warnings; use 5.010; use Data::Dumper; my $choices = [ [ 'a', 'W', 'interupt', ], [ 'b', 'R', 'interrupt', ], [ 'c', 'W', 'innterupt', ], [ 'd', 'W', 'intterupt', ] ]; my $answer = 'd'; # As we know that the answer matches only one choice # and we have exactly one matching answer. my $chosen_answer = (grep { $_->[0] eq $answer } @$choices)[0]; say Dumper($chosen_answer);

McA


Comment on Re^5: grep and dereferencing an array of arrays
Download Code
Re^6: grep and dereferencing an array of arrays
by choroba (Abbot) on Sep 06, 2013 at 21:04 UTC
    Or, maybe more readable for some:
    my ($chosen_answer) = grep { $_->[0] eq $answer } @$choices;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I have to agree that this is more elegant and oakb asked for an elegant solution. Eily proposed almost the same a little earlier.

      Probably I can argue that my solution is more explicit. ;)

      Regards
      McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-12-26 22:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls