Converting code to perl sub, but not sure I'm doing it right

Posted by Ben Dauphinee on Stack Overflow See other posts from Stack Overflow or by Ben Dauphinee
Published on 2010-06-13T14:55:51Z Indexed on 2010/06/13 15:02 UTC
Read the original article Hit count: 218

Filed under:
|

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'
});

© Stack Overflow or respective owner

Related posts about perl

Related posts about function