http://www.perlmonks.org?node_id=122500


in reply to Search an array ref of array refs

Thanks to the good 'ol folks at google and Perl Limericks:

my $domain_ip; map { if($_->[0] == $domainid){ $domain_ip = @$_->[2] } } @$table;

Tx all! :)

Replies are listed 'Best First'.
Re: Re: Search an array ref of array refs
by blakem (Monsignor) on Nov 01, 2001 at 08:49 UTC
    I think grep makes more sense in this case. Assuming you know there is only one result per $id, and don't mind checking the entire datastructure, this should work:
    my $domain_ip = (grep {$_->[0] == $domainid} @$table)[0]->[2];
    As in the folloing example:
    #!/usr/bin/perl -wT use strict; my $table = [ [7, 'tigers', 600], [8, 'elephants', 250], [9, 'lions', 500], ]; my $id = 8; my $fieldindex = 1; my $animal = (grep {$_->[0] == $id} @$table)[0]->[$fieldindex]; print "id=$id fieldindex=$fieldindex => animal=$animal\n"; =OUTPUT id=8 fieldindex=1 => animal=elephants

    -Blake