Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Duplicated data in array.

by LanX (Saint)
on Nov 02, 2021 at 19:31 UTC ( [id://11138346]=note: print w/replies, xml ) Need Help??


in reply to Duplicated data in array.

Your data is missing a curly again.

anyway, others may profit from this

use strict; use warnings; use Data::Dump qw/pp dd/; my @in = ( { CC => "X5667", ID => 11, Name => "Mary" }, { CC => "X456", ID => 10, Name => "Joe" }, { CC => "P45", ID => "X2", Name => "Carl" }, { CC => "PK1", ID => 12, Name => "Jim" }, { CC => "X5667", ID => 11, Name => "Mary" }, ); # --- Version1 ID deserves its name my %seen1; my @out1 = grep { ! $seen1{ $_->{ID} }++ } @in; pp \@out1; # --- Version2 combine values to one multi-dim key my %seen2; my @out2 = grep { ! $seen2{ $_->{ID}, $_->{Name}, $_->{CC} }++ } @in; pp \@out2;

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Duplicated data in array.
by Bod (Parson) on Nov 03, 2021 at 00:21 UTC
    Your data is missing a curly again

    Some would say consistency is a good thing...

      “A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines." – Ralph Waldo Emerson

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        I like many statements by Ralph Waldo Emerson, but in this case, I'd like to understand what a "foolish consistency" is (probably versus non-foolish consistency)
Re^2: Duplicated data in array.
by Anonymous Monk on Nov 02, 2021 at 20:11 UTC
    Would this be possible to be stored in reference? Getting confused with the data type.
    my %seen1; my $out1 = grep { ! $seen1{ $_->{ID} }++ } @in; pp @{ $out1 };

      Untested:

      my %seen1; my @out1 = grep { ! $seen1{ $_->{ID} }++ } @in; my $arrayref = \@out1; pp $arrayref;

      Update: Perhaps a bit late for this afterthought, but the general form of this question is "How do I create a reference to X?" Please see perlref and perlreftut. See also Perl Data Structures Cookbook (perldsc) for dealing with complicated referential structures.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-03-28 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found