1
Create a unique comma separated list from an array.
Hi There,
Some quick PERL today
– Recently I was working with someone who wanted to produce a comma separated list from an array element – the catch was they only wanted unique values. There attempt was as follows:
my ($val1,$val2,$val3) = @_;
my $return;
$return .= $val1;
$return .= ','.$val2 if ($val2 && ($val2 ne $val1));
$return .= ','.$val3 if ($val3 && ($val3 ne $val1) && ($val3 ne $val2));
return $return;
Okay, so that does the job. Is it pretty? no. Is it efficient? Hell no.
So, heres how I propose it should be done:
my @list = @_;
my %seen = ();
@list = grep { $_ && !$seen{$_}++ }@list;
return join(',',@list);
Beautiful, and only 2 lines of code.
. If any of you have a better of way of doing this, please feel free to comment below.












