in reply to Understanding how sort uniq works
Is it possible to achieve same logic with less lines or nicer solution?
Nicer is in the eye of the beholder. However, here's an SSCCE:
use strict; use warnings; use Test::More tests => 2; use List::MoreUtils 'uniq'; my $v = 'ovedpo15'; my $href = { $v => [5, 3, 2] }; pushsortuniq ($href->{$v}, 3); is_deeply ($href->{$v}, [2, 3, 5], 'Duplicate added, sorted'); pushsortuniq ($href->{$v}, 4); is_deeply ($href->{$v}, [2, 3, 4, 5], 'Non-duplicate added, sorted'); sub pushsortuniq { my ($aref, @topush) = @_; push @$aref, @topush; @$aref = sort (uniq(@$aref)); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Understanding how sort uniq works
by AnomalousMonk (Archbishop) on Jul 26, 2019 at 13:37 UTC |