Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Get just the unique value

by Anonymous Monk
on Apr 05, 2013 at 15:28 UTC ( #1027154=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!

I need to get only the unique value from this anonymous array of hashes, but I just donít understand why this code can't?
#/usr/bin/perl use strict; use Data::Dumper; my $data = [ { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Documents', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Money', 'reference' => '340 ', 'type' => 'Plastic' }, { 'name' => 'Money', 'reference' => '340 ', 'type' => 'Plastic' }, { 'name' => 'Money', 'reference' => '340 ', 'type' => 'Plastic' }, { 'name' => 'Money', 'reference' => '340 ', 'type' => 'Plastic' }, { 'name' => 'State', 'reference' => '40 ', 'type' => 'Cotton' }, { 'name' => 'State', 'reference' => '40 ', 'type' => 'Cotton' }, { 'name' => 'State', 'reference' => '40 ', 'type' => 'Cotton' }, { 'name' => 'Slice', 'reference' => '30 ', 'type' => 'Cotton' }, { 'name' => 'Part', 'reference' => '45 ', 'type' => 'Cotton' }, { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, { 'name' => 'Discount', 'reference' => '100 ', 'type' => 'Paper' }, ]; my $res; for my $entry (@$data) { #$entry->{type} = [] unless exists $entry->{type}; push @$res, $entry->{type} unless $entry; } print Dumper @$res;
Thanks for looking!

Comment on Get just the unique value
Download Code
Re: Get just the unique value
by CountOrlok (Friar) on Apr 05, 2013 at 15:49 UTC
    Use a hash instead:
    my %res = (); for my $entry (@$data) { $res{$entry->{type}} = 1; } print join(',', sort(keys(%res)))."\n";
Re: Get just the unique value
by hdb (Parson) on Apr 05, 2013 at 16:02 UTC

    Use a hash and build keys from the whole entry:

    my %res; for my $entry (@$data) { $res{join "|", sort %{$entry}} = $entry; } @$data = values %res; print Dumper $data;

    UPDATE: Added a "sort" to become independent from order within the hash.

      It still doesnt work, I am still getting duplicated "type":
      $VAR1 = [ { 'reference' => '100 ', 'name' => 'Discount', 'type' => 'Paper' }, { 'reference' => '30 ', 'name' => 'Slice', 'type' => 'Cotton' }, { 'reference' => '340 ', 'name' => 'Money', 'type' => 'Plastic' }, { 'reference' => '100 ', 'name' => 'Documents', 'type' => 'Paper' }, { 'reference' => '45 ', 'name' => 'Part', 'type' => 'Cotton' }, { 'reference' => '40 ', 'name' => 'State', 'type' => 'Cotton' } ];

        Well, these are the distinct records from your original array of hashes. You did not specify which value is to be unique. I would think that you should now be able from the answers here and the answers from your post yesterday: Accessing this array ref. to conclude whatever you wanted to achieve.

        It isn't clear what you want for output. You want to remove entries in the original data where type is duplicated, it seems, but how do you want to handle the case where reference or name is different? Which entry in the arrayref do you want to choose?
Re: Get just the unique value
by NetWallah (Monsignor) on Apr 05, 2013 at 17:06 UTC
    You could do it without naming a temporary hash - I'm not recommending you do this, but it was fun writing:
    print qq|$_ | for sort keys %{{ +map {$_->{type} => undef} @$data }};
    Prints:
    Cotton Paper Plastic

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Get just the unique value
by jaredor (Deacon) on Apr 05, 2013 at 18:26 UTC
    use Modern::Perl; use List::MoreUtils qw(uniq); use Data::Dump qw(pp); my $data = { ... your array of hashes here ... }; my @names = uniq map { $_->{'type'} } @$data; pp \@names;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-04-21 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls