I have an array of hashes and I need to get unique values for the college_name from this data structure.
I have achieved the same but looks like a long process.
use strict;
use warnings;
use Data::Dumper;
use List::MoreUtils qw(uniq);
my %col_hash = ();
my $college_ids = [
{
'term' => 'SPRING',
'city_code' => '530233',
'college_id' => '200',
'college_name' => 'Arts',
'course_name' => 'Drawing',
},
{
'term' => 'SUMMER',
'city_code' => '534233',
'college_id' => '300',
'college_name' => 'COMMERCE',
'course_name' => 'FINANCE',
}
];
foreach my $elem (@$college_ids) {
if (exists $col_hash{'college_name'}) {
push(@{ $col_hash{'college_name'} }, $elem->{'college_name'});
}
else {
$col_hash{'college_name'} = [$elem->{'college_name'}];
}
}
my @unique_college_names = uniq @{ $col_hash{'college_name'} };
warn Dumper(" LONG METHOD = ", @unique_college_names);
I have to do the same for Term, College_name, City code.
Is there an alternate method to achieve the same functionality?