Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks
How do I search an array for a pair of matching elements ?
Lets say you have an array as follows
@array=("host1","user1","info1","host2","user2","info2"...)
I wish to return the "info" element of the array when I'm given the "host" and "user".
e.g. I am given "host2","user2" and wish to return "info2" ?
Any help appreciated

Janitored by Arunbear - added code tags, and changed title from 'A searching question'

Replies are listed 'Best First'.
Re: Search an array for a pair of matching elements
by gaal (Parson) on Oct 26, 2004 at 14:45 UTC
    Whenever tuples of elements in an array have some relationship to each other.... think hashes.

    It sounds like the data structure you want is a hash of hashes, like so:

    $hostdata = { host1 => { user1 => "info1", }, host2 => { user2 => "info2", user17 => "info39", }, # ... };

    In this case, assuming userM exists on hostN, to get the info for that user you'd just use:

    $info = $hostdata->{hostN}->{userM};         # the second "->" is optional

    There are issues like this, such as autovivification, but you can read up on that if you like. (You should, actually.)

    And as for your original question: assuming data integrity, that is, the array size is a multiple of three, you can just run an index from 0 up, incrementing it by 3 every time, and return a result if ($array[$i] eq $host && $array[$i + 1] eq $user) .

      Thanks for your reply, I hadn't considered using a hash of hashes. This will help.
Re: Search an array for a pair of matching elements
by davido (Cardinal) on Oct 26, 2004 at 15:17 UTC

    If you must work with a flat datastructure such as a plain old array, then just use a binary search that limits itself to searching element 0, 3, 6, 9, 12, 15, .. n. You can google for binary search algorithms, and then just modify one to focus only on the first of each three elements. If the number of records never will grow large, a binary search may just be too cumbersome. If that's the case (that the array always stays pretty small), just search in such a way that each iteration skips two elements, so that you're framed to look at records.

    On the other hand, I'd say that you're just using the wrong datastructure. You probably should look into using a hash of hashes. You can read up on that sort of thing in the following documents: perlreftut, perldsc, and perllol. For more advanced reading, there's perlref.

    I hope this gives you enough to get started.


    Dave

Re: Search an array for a pair of matching elements
by gothic_mallard (Pilgrim) on Oct 26, 2004 at 14:34 UTC

    This sounds a little like a homework question which tend to be frowned upon here.

    Can you explain the logic behind this search (it's application in the real world etc). Also posting any work you've already attempted would be helpful. You're much more likely to get replies here if you say "I've tried X, Y and Z but can't understand why it doesn't work" rather than "do this for me".

    All I'd say in the mean time is read up on for loops which should put you on (one of the) right track to solving your question.

    --- Jay

    All code is untested unless otherwise stated.

Re: Search an array for a pair of matching elements
by TedPride (Priest) on Oct 26, 2004 at 21:35 UTC
    Hashes are much better if you're going to be doing more than one or two searches, but here you are:
    use strict; my @array=("host1","user1","info1","host2","user2","info2"); my $host = "host2"; my $user = "user2"; for (my $i = 0; $i < $#array; $i += 3) { if ($array[$i] eq $host && $array[$i+1] eq $user) { print $array[$i+2]; last; } }