Converting code to perl sub, but not sure I'm doing it right
- by Ben Dauphinee
I'm working from a question I posted earlier (here), and trying to convert the answer to a sub so I can use it multiple times. Not sure that it's done right though. Can anyone provide a better or cleaner sub?
sub search_for_key
{
my ($args) = @_;
foreach $row(@{$args->{search_ary}}){
print "@$row[0] : @$row[1]\n";
}
my $thiskey = NULL;
my @result = map { $args->{search_ary}[$_][0] } # Get the 0th column...
grep { @$args->{search_in} =~ /$args->{search_ary}[$_][1]/ } # ... of rows where the
0 .. $#array; # first row matches
$thiskey = @result;
print "\nReturning: " . $thiskey . "\n";
return $thiskey;
}
search_for_key({
'search_ary' => $ref_cam_make,
'search_in' => 'Canon EOS Rebel XSi'
});