Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Duplicated data in array.

by Anonymous Monk
on Nov 02, 2021 at 18:53 UTC ( #11138344=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

I need to get rid of the duplicates coming from an array, as a sample here is some data:

$VAR1 = { 'ID' => '11', 'Name' => 'Mary', 'CC' => 'X5667' }; $VAR2 = { 'ID' => '10', 'Name' => 'Joe', 'CC' => 'X456' }; $VAR3 = { 'ID' => 'X2', 'Name' => 'Carl', 'CC' => 'P45' }; $VAR4 = { 'ID' => '12', 'Name' => 'Jim', 'CC' => 'PK1' ; $VAR5 = { 'ID' => '11', 'Name' => 'Mary', 'CC' => 'X5667' };

Is there a better or correct way of doing this?
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; foreach my $st ( @{ $stuff } ) { next if $st->{ID}; next if $st->{Name}; next if $st->{CC}; print $st->{ID} - $st->{Name} - $st->{CC}; }

Thanks for looking!

Replies are listed 'Best First'.
Re: Duplicated data in array.
by LanX (Sage) on Nov 02, 2021 at 19:31 UTC
    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

      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.

      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:  <%-{-{-{-<

Re: Duplicated data in array.
by hippo (Bishop) on Nov 02, 2021 at 22:12 UTC
Re: Duplicated data in array.
by Anonymous Monk on Nov 02, 2021 at 19:07 UTC
    As usual you are showing nonsense code to pretend you made any effort.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-06-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (81 votes). Check out past polls.

    Notices?