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

I've got a hash reference to an array of more hashes.

 push(@{vars->{uplinks}}, $connection);

This little bit adds the hashes to the array. What I need to do is check the uplinks array for an existing connection. If there is already the same connection in the uplinks list, don't push.

I'm having trouble setting up a function to check this case since I'm dealing with such deep, anonymous, referenced data structures. Any wisdom I need to take in?

Ransom

Replies are listed 'Best First'.
Re: Hash of array of hash access
by sauoq (Abbot) on May 14, 2012 at 15:47 UTC
    What I need to do is check the uplinks array for an existing connection. If there is already an entry in the uplinks list, don't push.

    With your use of the phrase "an entry," it sounds like you are saying that you don't want to push if the array already has one or more members. I have a feeling that's not what you mean though. Do you mean that or do you mean that you don't want to push $connection onto your array if it already exists in your array?

    -sauoq
    "My two cents aren't worth a dime.";

      Sorry for not being clear. sauoq is correct, I would like to check if the particular $connection is already in the array before pushing.

      A recent change in the code brings some duplicate connections in. While this is technically correct, when displaying the results (the code I'm working on), I want just a single entry per unique connection.

      Ransom
Re: Hash of array of hash access
by stevieb (Canon) on May 14, 2012 at 15:44 UTC

    Untested:

    use List::MoreUtils qw(any); push @{$vars->{uplinks}}, $conn unless any { $conn eq $_ } @{$vars->{uplinks}};

    Update: After reading sauoq's reply below, I may have misunderstood the question. The OP will have to clarify. If they are saying that if there is *anything* in the array, don't push, but then it would be probably better to get rid of the array entirely as its really not needed.