mrguy123 has asked for the wisdom of the Perl Monks concerning the following question:
I want to make this list unique, meaning it needs to look like this:@students = ( { name => "Guy", college => "Yale", }, { name => "Gil", college => "Harvard", }, { name => "Gil", college => "Harvard", }, );
List::MoreUtils::uniq doesn't work on lists of hashes so that's a no go. I can do it for one hash key (below) but for 2 or more keys it's tricky@uniq_students = ( { name => "Guy", college => "Yale", }, { name => "Gil", college => "Harvard", }, );
Of course I can find a workaround (e.g. make each hash into a string and then sort ...) but I would like to try and find something better (and shorter) if possible##Solution for one hash key my @unique = do { my %seen; grep { !$seen{$_->{name}}++ } @students};
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Unique list of hashes
by hdb (Monsignor) on Mar 02, 2015 at 11:29 UTC | |
by mrguy123 (Hermit) on Mar 02, 2015 at 11:36 UTC | |
Re: Unique list of hashes
by LanX (Saint) on Mar 02, 2015 at 11:37 UTC | |
Re: Unique list of hashes
by parv (Parson) on Mar 02, 2015 at 11:41 UTC | |
by Anonymous Monk on Mar 02, 2015 at 11:57 UTC | |
by hdb (Monsignor) on Mar 02, 2015 at 12:05 UTC | |
by parv (Parson) on Mar 02, 2015 at 12:24 UTC |