Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Removing duplicate hashrefs from an array

by msomanat (Initiate)
on Oct 05, 2014 at 19:49 UTC ( [id://1102882]=perlquestion: print w/replies, xml ) Need Help??

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

I was trying to build an array of hashes and the code goes like this.
foreach my $network (@filtered_networks) { my $href; $href->{node} = $node->{hostname}; $href->{port} = $network->{interface}; unless ($href ~~ @ret_array) { push (@ret_array, $href); } }
The perl version is 5.14 which supports "~~" But still it wont remove the duplicate entries. My dumper looks like:
2501 Return array is $VAR1 = { 2502 'node' => 'cst-2554A-004-05', 2503 'port' => 'e0b' 2504 }; 2505 $VAR2 = { 2506 'node' => 'cst-2554A-004-05', 2507 'port' => 'e0b' 2508 }; 2509 $VAR3 = { 2510 'node' => 'cst-2554A-004-05', 2511 'port' => 'e0a' 2512 };
Please help me in understanding what obvious mistake I am making. Thanking in advance.

Replies are listed 'Best First'.
Re: Removing duplicate hashrefs from an array
by choroba (Cardinal) on Oct 05, 2014 at 20:01 UTC
    The smartmatch table in perlop explains what ~~ does for a HASH and ARRAY:
    HASH ~~ ARRAY : any ARRAY elements exist as HASH keys (like: grep { exists HASH->{$_} } ARRAY)

    Update: If you want to get unique nodes and ports, use a hash of hashes:

    $href->{$node}{$port} = 1;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Removing duplicate hashrefs from an array
by LanX (Saint) on Oct 05, 2014 at 20:34 UTC
      Thank you LanX. I should have thought on that - the uniqueness of the href. However, as you suggested, I concatenated the values, and that did the trick. :) But, there should be a straight forward method to compare these hashes within the array. Anyway, Thanking everyone for the suggestions.
        > But, there should be a straight forward method to compare these hashes within the array

        Look the point is that your whole data structure is not straight forward.

        Why an AoH if you want uniqueness?

        And which entry should be unique, really both or all??

        Using %seen if common knowledge (see FAQ) and "combined uniqueness" needs combination, that's to unusual for a special operator and straight forward to implement.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Removing duplicate hashrefs from an array
by Laurent_R (Canon) on Oct 05, 2014 at 21:01 UTC
    Hmm, I have almost never used the smart match operator. For a very long time, I could not use it because I could not upgrade to a Perl version supporting it. By the time I could finally upgrade, the smart match operator had become deprecated (or, rather, labeled, experimental and subject to changes), so that I prefer not to use it, and you should probably do the same.

    Having said that, and keeping in mind that I've never used smart match for anything but just quickly testing it out of curiosity, I would think that comparing an array with an hash ref (i.e. a scalar containing a reference to an hash) is probably unlikely to work. Maybe you would need to dereference the hashref before using it. But I may be completely wrong on that. An additional consideration is that your hash or hashref should be declared before entering the foreach loop.

    Anyway, given the experimental nature of the smart match, using a hash or a hash of hashes (depending on what type of uniqueness you exactly need) in the traditional idiomatic Perl way is likely to be a more robust and lasting solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102882]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found