http://www.perlmonks.org?node_id=1027192


in reply to Re^4: Get just the unique value
in thread Get just the unique value

I think if you feed your new structure to some of the proposals it will work...

Replies are listed 'Best First'.
Re^6: Get just the unique value
by Anonymous Monk on Apr 05, 2013 at 18:24 UTC
    I tested, none of them work unfortunately, I will look also where.
      Maybe if you gave us the desired output...
        Sure, the end result is that $data only holds the unique values: From this:
        my $data = [ { 'type' => 'Paper' }, { 'type' => 'Paper' }, { 'type' => 'Plastic' }, { 'type' => 'Plastic' }, { 'type' => 'Plastic' }, { 'type' => 'Plastic' }, { 'type' => 'Cotton' }, { 'type' => 'Cotton' }, { 'type' => 'Cotton' }, { 'type' => 'Cotton' }, { 'type' => 'Cotton' }, { 'type' => 'Paper' }, { 'type' => 'Paper' }, { 'type' => 'Paper' }, { 'type' => 'Paper' }, { 'type' => 'Paper' }, ];
        To that:
        $data = [ { 'type' => 'Paper' }, { 'type' => 'Plastic' }, { 'type' => 'Cotton' }, ];

      How about this?

      my %types; my $res; foreach my $entry (@$data) { push @$res, $entry unless $types{$entry->{"type"}}++; } print Dumper $res;

        Or this without helper hash?

        my $res; foreach my $entry (@$data) { push @$res, $entry unless grep { $_->{"type"} eq $entry->{"type"} +} @$res; } print Dumper $res;