Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

    -Blake

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-01-22 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (238 votes). Check out past polls.

    Notices?