
BillKSmith
Veteran
Sep 25, 2013, 10:14 AM
Views: 2653
|
Re: [sriharsha_12] Search Hash of Arrays - using Grep
|
|
|
It requires nested greps.
use strict; use warnings; my %hashSample = ( 1 => ["AB", "XY"], 2 => ["BC", "YZ"], 3 => ["CA", "ZX"], ); (my $hashkey) = grep { grep {$_ eq 'AB'} @{$hashSample{$_}}} keys %hashSample; print $hashkey, "\n"; Note: The outer grep must be in list context even if you only want one key. If there is more than one match, this code will return the first of them. Good Luck, Bill
|