Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Search an array ref of array refs

by bittondb (Novice)
on Nov 01, 2001 at 08:22 UTC ( #122500=note: print w/replies, xml ) Need Help??

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://122500]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-07-17 10:10 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (363 votes). Check out past polls.