summaryrefslogtreecommitdiffstats
path: root/MDK/Common/DataStructure.pm
diff options
context:
space:
mode:
authorGuillaume Cottenceau <gc@mandriva.com>2003-10-15 15:15:07 +0000
committerGuillaume Cottenceau <gc@mandriva.com>2003-10-15 15:15:07 +0000
commitd7f24c815b6ee64ad2d7e0a374d593f1ecf6e301 (patch)
tree07360cc861cf9ee4036b4f58296ebc30b929746d /MDK/Common/DataStructure.pm
parentbe8f7e12a07c1f69d159516c08760b686d9d3b31 (diff)
downloadperl-MDK-Common-d7f24c815b6ee64ad2d7e0a374d593f1ecf6e301.tar
perl-MDK-Common-d7f24c815b6ee64ad2d7e0a374d593f1ecf6e301.tar.gz
perl-MDK-Common-d7f24c815b6ee64ad2d7e0a374d593f1ecf6e301.tar.bz2
perl-MDK-Common-d7f24c815b6ee64ad2d7e0a374d593f1ecf6e301.tar.xz
perl-MDK-Common-d7f24c815b6ee64ad2d7e0a374d593f1ecf6e301.zip
add uniq_ (uniq but according to some code results on each value)
Diffstat (limited to 'MDK/Common/DataStructure.pm')
-rw-r--r--MDK/Common/DataStructure.pm16
1 files changed, 15 insertions, 1 deletions
diff --git a/MDK/Common/DataStructure.pm b/MDK/Common/DataStructure.pm
index 195f4cb..ee649ea 100644
--- a/MDK/Common/DataStructure.pm
+++ b/MDK/Common/DataStructure.pm
@@ -72,6 +72,14 @@ is the scalar undefined or is the hash empty
returns the list with no duplicates (keeping the first elements)
+=item uniq_ { CODE } LIST
+
+returns the list with no duplicates according to the scalar results of CODE on each element of LIST (keeping the first elements)
+
+ uniq_ { $_->[1] } [ 1, "fo" ], [ 2, "fob" ], [ 3, "fo" ], [ 4, "bar" ]
+
+gives [ 1, "fo" ], [ 2, "fob" ], [ 4, "bar" ]
+
=item difference2(ARRAY REF, ARRAY REF)
returns the first list without the element of the second list
@@ -111,7 +119,7 @@ use MDK::Common::Func;
use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK);
@ISA = qw(Exporter);
-@EXPORT_OK = qw(sort_numbers ikeys add2hash add2hash_ put_in_hash member invbool listlength deref deref_array is_empty_array_ref is_empty_hash_ref uniq difference2 intersection next_val_in_array group_by2 list2kv);
+@EXPORT_OK = qw(sort_numbers ikeys add2hash add2hash_ put_in_hash member invbool listlength deref deref_array is_empty_array_ref is_empty_hash_ref uniq uniq_ difference2 intersection next_val_in_array group_by2 list2kv);
%EXPORT_TAGS = (all => [ @EXPORT_OK ]);
@@ -134,6 +142,12 @@ sub uniq { my %l; $l{$_} = 1 foreach @_; grep { delete $l{$_} } @_ }
sub difference2 { my %l; @l{@{$_[1]}} = (); grep { !exists $l{$_} } @{$_[0]} }
sub intersection { my (%l, @m); @l{@{shift @_}} = (); foreach (@_) { @m = grep { exists $l{$_} } @$_; %l = (); @l{@m} = () } keys %l }
+sub uniq_(&@) {
+ my $f = shift;
+ $l{$f->($_)} = 1 foreach @_;
+ grep { delete $l{$f->($_)} } @_;
+}
+
sub next_val_in_array {
my ($v, $l) = @_;