1Hash::MoreUtils(3) User Contributed Perl Documentation Hash::MoreUtils(3)
2
3
4
6 Hash::MoreUtils - Provide the stuff missing in Hash::Util
7
9 use Hash::MoreUtils qw(slice slice_def slice_exists slice_grep
10 hashsort
11 );
12
14 Similar to "List::MoreUtils", "Hash::MoreUtils" contains trivial but
15 commonly-used functionality for hashes.
16
18 "slice" HASHREF[, LIST]
19 Returns a hash containing the (key, value) pair for every key in LIST.
20
21 "slice_def" HASHREF[, LIST]
22 As "slice", but only includes keys whose values are defined.
23
24 "slice_exists" HASHREF[, LIST]
25 As "slice" but only includes keys which exist in the hashref.
26
27 "slice_grep" BLOCK, HASHREF[, LIST]
28 As "slice", with an arbitrary condition.
29
30 Unlike "grep", the condition is not given aliases to elements of
31 anything. Instead, %_ is set to the contents of the hashref, to avoid
32 accidentally auto-vivifying when checking keys or values. Also,
33 'uninitialized' warnings are turned off in the enclosing scope.
34
35 "hashsort" [BLOCK,] HASHREF
36 my @array_of_pairs = hashsort \%hash;
37 my @pairs_by_length = hashsort sub { length($a) <=> length($b) }, \%hash;
38
39 Returns the (key, value) pairs of the hash, sorted by some property of
40 the keys. By default (if no sort block given), sorts the keys with
41 "cmp".
42
43 I'm not convinced this is useful yet. If you can think of some way it
44 could be more so, please let me know.
45
46 "safe_reverse" [BLOCK,] HASHREF
47 my %dup_rev = safe_reverse \%hash
48
49 sub croak_dup {
50 my ($k, $v, $r) = @_;
51 exists( $r->{$v} ) and
52 croak "Cannot safe reverse: $v would be mapped to both $k and $r->{$v}";
53 $v;
54 };
55 my %easy_rev = save_reverse \&croak_dup, \%hash
56
57 Returns safely reversed hash (value, key pairs of original hash). If no
58 "BLOCK" is given, following routine will be used:
59
60 sub merge_dup {
61 my ($k, $v, $r) = @_;
62 return exists( $r->{$v} )
63 ? ( ref($r->{$v}) ? [ @{$r->{$v}}, $k ] : [ $r->{$v}, $k ] )
64 : $k;
65 };
66
67 The "BLOCK" will be called with 3 arguments:
68
69 "key" The key from the "( key, value )" pair in the original hash
70
71 "value" The value from the "( key, value )" pair in the original hash
72
73 "ref-hash"
74 Reference to the reversed hash (read-only)
75
76 The "BLOCK" is expected to return the value which will used for the
77 resulting hash.
78
80 Hans Dieter Pearcey, "<hdp@cpan.org>" Jens Rehsack,
81 "<rehsack@cpan.org>"
82
84 Please report any bugs or feature requests to
85 "bug-hash-moreutils@rt.cpan.org", or through the web interface at
86 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-MoreUtils
87 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-MoreUtils>. I
88 will be notified, and then you'll automatically be notified of progress
89 on your bug as I make changes.
90
92 You can find documentation for this module with the perldoc command.
93
94 perldoc Hash::MoreUtils
95
96 You can also look for information at:
97
98 · RT: CPAN's request tracker
99
100 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-MoreUtils
101 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-MoreUtils>
102
103 · AnnoCPAN: Annotated CPAN documentation
104
105 http://annocpan.org/dist/Hash-MoreUtils
106 <http://annocpan.org/dist/Hash-MoreUtils>
107
108 · CPAN Ratings
109
110 http://cpanratings.perl.org/d/Hash-MoreUtils
111 <http://cpanratings.perl.org/d/Hash-MoreUtils>
112
113 · Search CPAN
114
115 http://search.cpan.org/dist/Hash-MoreUtils/
116 <http://search.cpan.org/dist/Hash-MoreUtils/>
117
120 Copyright 2005 Hans Dieter Pearcey, all rights reserved. Copyright
121 2010 Jens Rehsack
122
123 This program is free software; you can redistribute it and/or modify it
124 under the terms of either: the GNU General Public License as published
125 by the Free Software Foundation; or the Artistic License.
126
127 See http://dev.perl.org/licenses/ for more information.
128
129
130
131perl v5.12.3 2011-03-14 Hash::MoreUtils(3)