From cec3bf7ceade91064ce3d87122c5aa0967e23a55 Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Sun, 27 Jan 2002 12:38:18 +0000 Subject: add MDK::Common::DataStructure::group_by2 --- MDK/Common/DataStructure.pm | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'MDK') diff --git a/MDK/Common/DataStructure.pm b/MDK/Common/DataStructure.pm index 83e49f1..e4b04b4 100644 --- a/MDK/Common/DataStructure.pm +++ b/MDK/Common/DataStructure.pm @@ -73,10 +73,15 @@ finds the value that follow the scalar in the list (circular): C gives C<1> (do not use a list with duplicates) +=item group_by2(LIST) + +interprets the list as an ordered hash, returns a list of [key,value]: +C 2, 3 => 4, 5 => 6)> gives C<[1,2], [3,4], [5,6]> + =item list2kv(LIST) -interprets the list as an hash, returns the keys and the values: -C 2, 3 => 4)> gives C<[1,3], [2,4]> +interprets the list as an ordered hash, returns the keys and the values: +C 2, 3 => 4, 5 => 6)> gives C<[1,3,5], [2,4,6]> =back @@ -94,7 +99,7 @@ use MDK::Common::Func; use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK); @ISA = qw(Exporter); -@EXPORT_OK = qw(ikeys add2hash add2hash_ put_in_hash member invbool listlength deref is_empty_array_ref is_empty_hash_ref uniq difference2 intersection next_val_in_array list2kv); +@EXPORT_OK = qw(ikeys add2hash add2hash_ put_in_hash member invbool listlength deref is_empty_array_ref is_empty_hash_ref uniq difference2 intersection next_val_in_array group_by2 list2kv); %EXPORT_TAGS = (all => [ @EXPORT_OK ]); @@ -132,4 +137,13 @@ sub list2kv { \@k, \@v; } +sub group_by2 { + my @l; + for (my $i = 0; $i < @_ ; $i += 2) { + push @l, [ $_[$i], $_[$i+1] ]; + } + @l; +} + + 1; -- cgit v1.2.1