Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^6: Get just the unique value

by Anonymous Monk
on Apr 05, 2013 at 18:24 UTC ( #1027193=note: print w/ replies, xml ) Need Help??


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

I tested, none of them work unfortunately, I will look also where.


Comment on Re^6: Get just the unique value
Replies are listed 'Best First'.
Re^7: Get just the unique value
by CountOrlok (Friar) on Apr 05, 2013 at 18:27 UTC
    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' }, ];
        hdb's code does exactly that, albeit sorted.
Re^7: Get just the unique value
by hdb (Prior) on Apr 05, 2013 at 19:34 UTC

    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;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2015-07-08 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls