1List::Compare::FunctionUasle(r3)Contributed Perl DocumenLtiastti:o:nCompare::Functional(3)
2
3
4
6 List::Compare::Functional - Compare elements of two or more lists
7
9 This document refers to version 0.33 of List::Compare::Functional.
10 This version was released February 19, 2006. The first released ver‐
11 sion of List::Compare::Functional was v0.21. Its version numbers are
12 set to be consistent with the other parts of the List::Compare distri‐
13 bution.
14
15 Notice of Interface Changes
16
17 Certain significant changes to the interface to List::Compare::Func‐
18 tional were made with the introduction of Version 0.25 in April 2004.
19 The documentation immediately below reflects those changes, so if you
20 are first using this module with that or a later version, simply read
21 and follow the documentation below. If, however, you used List::Com‐
22 pare::Functional prior to that version, see the discussion of interface
23 changes farther below: April 2004 Change of Interface.
24
26 Getting Started
27
28 List::Compare::Functional exports no subroutines by default.
29
30 use List::Compare::Functional qw(:originals :aliases);
31
32 will import all publicly available subroutines from List::Com‐
33 pare::Functional. The model for importing just one subroutine from
34 List::Compare::Functional is:
35
36 use List::Compare::Functional qw( get_intersection );
37
38 It will probably be most convenient for the user to import functions by
39 using one of the two following export tags:
40
41 use List::Compare::Functional qw(:main :mainrefs);
42
43 The assignment of the various comparison functions to export tags is
44 discussed below.
45
46 For clarity, we shall begin by discussing comparisons of just two lists
47 at a time. Farther below, we shall discuss comparisons among three or
48 more lists at a time.
49
50 Comparing Two Lists Held in Arrays
51
52 · Given two lists:
53
54 @Llist = qw(abel abel baker camera delta edward fargo golfer);
55 @Rlist = qw(baker camera delta delta edward fargo golfer hilton);
56
57 · Get those items which appear at least once in both lists (their
58 intersection).
59
60 @intersection = get_intersection( [ \@Llist, \@Rlist ] );
61
62 Note that you could place the references to the lists being com‐
63 pared into a named array and then pass "get_intersection()" a ref‐
64 erence to that array.
65
66 @to_be_compared = ( \@Llist, \@Rlist );
67 @intersection = get_intersection( \@to_be_compared );
68
69 Beginning with version 0.29 (May 2004), List::Compare::Functional
70 now offers an additional way of passing arguments to its various
71 functions. If you prefer to see a more explicit delineation among
72 the types of arguments passed to a function, pass a single hash
73 reference which holds the lists being compared in an anonymous
74 array which is the value corresponding to key "lists":
75
76 @intersection = get_intersection( {
77 lists => [ \@Llist, \@Rlist ],
78 } );
79
80 · Get those items which appear at least once in either list (their
81 union).
82
83 @union = get_union( [ \@Llist, \@Rlist ] );
84
85 or
86
87 @union = get_union( { lists => [ \@Llist, \@Rlist ] } );
88
89 · Get those items which appear (at least once) only in the first
90 list.
91
92 @Lonly = get_unique( [ \@Llist, \@Rlist ] );
93
94 or
95
96 @Lonly = get_unique( { lists => [ \@Llist, \@Rlist ] } );
97
98 · Get those items which appear (at least once) only in the second
99 list.
100
101 @Ronly = get_complement( [ \@Llist, \@Rlist ] );
102
103 or
104
105 @Ronly = get_complement( { lists => [ \@Llist, \@Rlist ] } );
106
107 ·
108 @LorRonly = get_symmetric_difference( [ \@Llist, \@Rlist ] );
109
110 @LorRonly = get_symdiff( [ \@Llist, \@Rlist ] ); # alias
111
112 or
113
114 @LorRonly = get_symmetric_difference( { lists => [ \@Llist, \@Rlist ] } );
115
116 · Make a bag of all those items in both lists. The bag differs from
117 the union of the two lists in that it holds as many copies of indi‐
118 vidual elements as appear in the original lists.
119
120 @bag = get_bag( [ \@Llist, \@Rlist ] );
121
122 or
123
124 @bag = get_bag( { lists => [ \@Llist, \@Rlist ] } );
125
126 · An alternative approach to the above functions: If you do not
127 immediately require an array as the return value of the function
128 call, but simply need a reference to an (anonymous) array, use one
129 of the following parallel functions:
130
131 $intersection_ref = get_intersection_ref( [ \@Llist, \@Rlist ] );
132 $union_ref = get_union_ref( [ \@Llist, \@Rlist ] );
133 $Lonly_ref = get_unique_ref( [ \@Llist, \@Rlist ] );
134 $Ronly_ref = get_complement_ref( [ \@Llist, \@Rlist ] );
135 $LorRonly_ref = get_symmetric_difference_ref( [ \@Llist, \@Rlist ] );
136 $LorRonly_ref = get_symdiff_ref( [ \@Llist, \@Rlist ] );
137 # alias
138 $bag_ref = get_bag_ref( [ \@Llist, \@Rlist ] );
139
140 or
141
142 $intersection_ref =
143 get_intersection_ref( { lists => [ \@Llist, \@Rlist ] } );
144 $union_ref =
145 get_union_ref( { lists => [ \@Llist, \@Rlist ] } );
146 $Lonly_ref =
147 get_unique_ref( { lists => [ \@Llist, \@Rlist ] } );
148 $Ronly_ref =
149 get_complement_ref( { lists => [ \@Llist, \@Rlist ] } );
150 $LorRonly_ref =
151 get_symmetric_difference_ref( { lists => [ \@Llist, \@Rlist ] } );
152 $LorRonly_ref =
153 get_symdiff_ref( { lists => [ \@Llist, \@Rlist ] } );
154 # alias
155 $bag_ref =
156 get_bag_ref( { lists => [ \@Llist, \@Rlist ] } );
157
158 · Return a true value if the first list ('L' for 'left') is a subset
159 of the second list ('R' for 'right').
160
161 $LR = is_LsubsetR( [ \@Llist, \@Rlist ] );
162
163 or
164
165 $LR = is_LsubsetR( { lists => [ \@Llist, \@Rlist ] } );
166
167 · Return a true value if R is a subset of L.
168
169 $RL = is_RsubsetL( [ \@Llist, \@Rlist ] );
170
171 or
172
173 $RL = is_RsubsetL( { lists => [ \@Llist, \@Rlist ] } );
174
175 · Return a true value if L and R are equivalent, i.e., if every ele‐
176 ment in L appears at least once in R and vice versa.
177
178 $eqv = is_LequivalentR( [ \@Llist, \@Rlist ] );
179 $eqv = is_LeqvlntR( [ \@Llist, \@Rlist ] ); # alias
180
181 or
182
183 $eqv = is_LequivalentR( { lists => [ \@Llist, \@Rlist ] } );
184
185 · Return a true value if L and R are disjoint, i.e., if L and R have
186 no common elements.
187
188 $disj = is_LdisjointR( [ \@Llist, \@Rlist ] );
189
190 or
191
192 $disj = is_LdisjointR( { lists => [ \@Llist, \@Rlist ] } );
193
194 · Pretty-print a chart showing whether one list is a subset of the
195 other.
196
197 print_subset_chart( [ \@Llist, \@Rlist ] );
198
199 or
200
201 print_subset_chart( { lists => [ \@Llist, \@Rlist ] } );
202
203 · Pretty-print a chart showing whether the two lists are equivalent
204 (same elements found at least once in both).
205
206 print_equivalence_chart( [ \@Llist, \@Rlist ] );
207
208 or
209
210 print_equivalence_chart( { lists => [ \@Llist, \@Rlist ] } );
211
212 · Determine in which (if any) of the lists a given string can be
213 found. In list context, return a list of those indices in the
214 argument list corresponding to lists holding the string being
215 tested.
216
217 @memb_arr = is_member_which( [ \@Llist, \@Rlist ] , [ 'abel' ] );
218
219 or
220
221 @memb_arr = is_member_which( {
222 lists => [ \@Llist, \@Rlist ], # value is array reference
223 item => 'abel', # value is string
224 } );
225
226 In the example above, @memb_arr will be:
227
228 ( 0 )
229
230 because 'abel' is found only in @Al which holds position 0 in the
231 list of arguments passed to "new()".
232
233 · As with other List::Compare::Functional functions which return a
234 list, you may wish the above function returned a (scalar) reference
235 to an array holding the list:
236
237 $memb_arr_ref = is_member_which_ref( [ \@Llist, \@Rlist ] , [ 'baker' ] );
238
239 or
240
241 $memb_arr_ref = is_member_which_ref( {
242 lists => [ \@Llist, \@Rlist ], # value is array reference
243 item => 'baker', # value is string
244 } );
245
246 In the example above, $memb_arr_ref will be:
247
248 [ 0, 1 ]
249
250 because 'baker' is found in @Llist and @Rlist, which hold positions
251 0 and 1, respectively, in the list of arguments passed to "new()".
252
253 Note: functions "is_member_which()" and "is_member_which_ref" test
254 only one string at a time and hence take only one argument. To
255 test more than one string at a time see the next function,
256 "are_members_which()".
257
258 · Determine in "which" (if any) of the lists passed as arguments one
259 or more given strings can be found. The lists beings searched are
260 placed in an array, a reference to which is the first argument
261 passed to "are_members_which()". The strings to be tested are also
262 placed in an array, a reference to which is the second argument
263 passed to that function.
264
265 $memb_hash_ref =
266 are_members_which( [ \@Llist, \@Rlist ] ,
267 [ qw⎪ abel baker fargo hilton zebra ⎪ ]
268 );
269
270 or
271
272 $memb_hash_ref = are_members_which( {
273 lists => [ \@Llist, \@Rlist ], # value is arrayref
274 items => [ qw⎪ abel baker fargo hilton zebra ⎪ ], # value is arrayref
275 } );
276
277 The return value is a reference to a hash of arrays. The key for
278 each element in this hash is the string being tested. Each ele‐
279 ment's value is a reference to an anonymous array whose elements
280 are those indices in the constructor's argument list corresponding
281 to lists holding the strings being tested. In the examples above,
282 $memb_hash_ref will be:
283
284 {
285 abel => [ 0 ],
286 baker => [ 0, 1 ],
287 fargo => [ 0, 1 ],
288 hilton => [ 1 ],
289 zebra => [ ],
290 };
291
292 Note: "are_members_which()" can take more than one argument;
293 "is_member_which()" and "is_member_which_ref()" each take only one
294 argument. Unlike those functions, "are_members_which()" returns a
295 hash reference.
296
297 · Determine whether a given string can be found in any of the lists
298 passed as arguments. Return 1 if a specified string can be found
299 in any of the lists and 0 if not.
300
301 $found = is_member_any( [ \@Llist, \@Rlist ] , [ 'abel' ] );
302
303 or
304
305 $found = is_member_any( {
306 lists => [ \@Llist, \@Rlist ], # value is array reference
307 item => 'abel', # value is string
308 } );
309
310 In the example above, $found will be 1 because 'abel' is found in
311 one or more of the lists passed as arguments to "new()".
312
313 · Determine whether a specified string or strings can be found in any
314 of the lists passed as arguments. The lists beings searched are
315 placed in an array, a reference to which is the first argument
316 passed to "are_members_any()". The strings to be tested are also
317 placed in an array, a reference to which is the second argument
318 passed to that function.
319
320 $memb_hash_ref =
321 are_members_any( [ \@Llist, \@Rlist ] ,
322 [ qw⎪ abel baker fargo hilton zebra ⎪ ]
323 );
324
325 or
326
327 $memb_hash_ref = are_members_any( {
328 lists => [ \@Llist, \@Rlist ], # value is arrayref
329 items => [ qw⎪ abel baker fargo hilton zebra ⎪ ], # value is arrayref
330 } );
331
332 The return value is a reference to a hash where an element's key is
333 the string being tested and the element's value is 1 if the string
334 can be found in any of the lists and 0 if not. In the examples
335 above, $memb_hash_ref will be:
336
337 {
338 abel => 1,
339 baker => 1,
340 fargo => 1,
341 hilton => 1,
342 zebra => 0,
343 };
344
345 "zebra"'s value is 0 because "zebra" is not found in either of the
346 lists passed as arguments to "are_members_any()".
347
348 · Return current List::Compare::Functional version number.
349
350 $vers = get_version;
351
352 Comparing Three or More Lists Held in Arrays
353
354 Given five lists:
355
356 @Al = qw(abel abel baker camera delta edward fargo golfer);
357 @Bob = qw(baker camera delta delta edward fargo golfer hilton);
358 @Carmen = qw(fargo golfer hilton icon icon jerky kappa);
359 @Don = qw(fargo icon jerky);
360 @Ed = qw(fargo icon icon jerky);
361
362 · Get those items which appear at least once in each list (their
363 intersection).
364
365 @intersection = get_intersection( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
366
367 or
368
369 @intersection = get_intersection( {
370 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
371 } );
372
373 · Get those items which appear at least once in any of the lists
374 (their union).
375
376 @union = get_union( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
377
378 or
379 @union = get_union( {
380 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
381 } );
382
383 · To get those items which are unique to a particular list, provide
384 "get_unique()" with two array references. The first holds refer‐
385 ences to the arrays which in turn hold the individual lists being
386 compared. The second holds the index position in the first refer‐
387 ence of the particular list under consideration. Example: To get
388 elements unique to @Carmen:
389
390 @Lonly = get_unique(
391 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
392 [ 2 ]
393 );
394
395 or
396
397 @Lonly = get_unique( {
398 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
399 item => 2, # value is number
400 } );
401
402 If no index position is passed to "get_unique()" it will default to
403 0 and report items unique to the first list passed to the function.
404 Hence,
405
406 @Lonly = get_unique( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
407
408 is same as:
409
410 @Lonly = get_unique( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [ 0 ] );
411
412 · Should you need to identify the items unique to each of the lists
413 under consideration, call "get_unique_all" and get a reference to
414 an array of array references:
415
416 $unique_all_ref = get_unique_all(
417 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ]
418 );
419
420 or
421
422 $unique_all_ref = get_unique_all( {
423 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
424 } );
425
426 · To get those items which appear only in lists other than one par‐
427 ticular list, pass two array references to the "get_complement()"
428 function. The first holds references to the arrays which in turn
429 hold the individual lists being compared. The second holds the
430 index position in the first reference of the particular list under
431 consideration. Example: to get all the elements found in lists
432 other than @Don:
433
434 @Ronly = get_complement(
435 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
436 [ 3 ]
437 );
438
439 or
440
441 @Ronly = get_complement( {
442 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
443 item => 3, # value is number
444 } );
445
446 If no index position is passed to "get_complement()" it will
447 default to 0 and report items found in all lists other than the
448 first list passed to "get_complement()".
449
450 @Lonly = get_complement( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
451
452 is same as:
453
454 @Lonly = get_complement( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [ 0 ] );
455
456 · Should you need to identify the items not found in each of the
457 lists under consideration, call "get_complement_all" and get a ref‐
458 erence to an array of array references:
459
460 $complement_all_ref = get_complement_all(
461 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ]
462 );
463
464 or
465
466 $complement_all_ref = get_complement_all( {
467 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
468 } );
469
470 · Get those items which do not appear in more than one of several
471 lists (their symmetric_difference);
472
473 @LorRonly = get_symmetric_difference( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
474 @LorRonly = get_symdiff( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] ); # alias
475
476 or
477
478 @LorRonly = get_symmetric_difference( {
479 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
480 } );
481
482 · Get those items found in any of several lists which do not appear
483 in "all" of the lists (i.e., all items except those found in the
484 intersection of the lists):
485
486 @nonintersection = get_nonintersection(
487 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
488
489 or
490
491 @nonintersection = get_nonintersection( {
492 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
493 } );
494
495 · Get those items which appear in more than one of several lists
496 (i.e., all items except those found in their symmetric difference);
497
498 @shared = get_shared( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
499
500 or
501
502 @shared = get_shared( {
503 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
504 } );
505
506 · Make a bag of every item found in every list. The bag differs from
507 the union of the two lists in that it holds as many copies of indi‐
508 vidual elements as appear in the original lists.
509
510 @bag = get_bag( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
511
512 or
513
514 @bag = get_bag( {
515 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
516 } );
517
518 · An alternative approach to the above functions: If you do not
519 immediately require an array as the return value of the function,
520 but simply need a reference to an array, use one of the following
521 parallel functions:
522
523 $intersection_ref = get_intersection_ref(
524 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
525 $union_ref = get_union_ref(
526 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
527 $Lonly_ref = get_unique_ref(
528 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
529 $Ronly_ref = get_complement_ref(
530 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
531 $LorRonly_ref = get_symmetric_difference_ref(
532 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
533 $LorRonly_ref = get_symdiff_ref( # alias
534 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
535 $nonintersection_ref = get_nonintersection_ref(
536 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
537 $shared_ref = get_shared_ref(
538 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
539 $bag_ref = get_bag_ref(
540 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
541
542 · To determine whether one particular list is a subset of another of
543 the lists passed to the function, pass to "is_LsubsetR()" two array
544 references. The first of these is a reference to an array of array
545 references, the arrays holding the lists under consideration. The
546 second is a reference to a two-element array consisting of the
547 index of the presumed subset, followed by the index position of the
548 presumed superset. A true value (1) is returned if the first
549 (left-hand) element in the second reference list is a subset of the
550 second (right-hand) element; a false value (0) is returned other‐
551 wise.
552
553 Example: To determine whether @Ed is a subset of @Carmen, call:
554
555 $LR = is_LsubsetR(
556 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
557 [ 4, 2 ]
558 );
559
560 or
561
562 $LR = is_LsubsetR( {
563 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
564 pair => [ 4, 2 ], # value is arrayref
565 } );
566
567 If only the first reference (to the array of lists) is passed to
568 "is_LsubsetR", then the function's second argument defaults to
569 "(0,1)" and compares the first two lists passed to the constructor.
570 So,
571
572 $LR = is_LsubsetR([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
573
574 ... is equivalent to:
575
576 $LR = is_LsubsetR([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [0,1] );
577
578 · To reverse the order in which the particular lists are evaluated
579 for superset/subset status, call "is_RsubsetL":
580
581 $RL = is_RsubsetL([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [2,4] );
582
583 or
584
585 $RL = is_RsubsetL( {
586 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
587 pair => [ 2, 4 ],
588 } );
589
590 · List::Compare::Functional considers two lists to be equivalent if
591 every element in one list appears at least once in R and vice
592 versa. To determine whether one particular list passed to the
593 function is equivalent to another of the lists passed to the func‐
594 tion, provide "is_LequivalentR()" with two array references. The
595 first is a reference to an array of array references, the arrays
596 holding the lists under consideration. The second of these is a
597 reference to a two-element array consisting of the two lists being
598 tested for equivalence. A true value (1) is returned if the lists
599 are equivalent; a false value (0) is returned otherwise.
600
601 Example: To determine whether @Don and @Ed are equivalent, call:
602
603 $eqv = is_LequivalentR(
604 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
605 [3,4]
606 );
607
608 $eqv = is_LeqvlntR( # alias
609 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
610 [3,4]
611 );
612
613 or
614
615 $eqv = is_LequivalentR( {
616 items => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
617 pair => [3,4],
618 } );
619
620 If no arguments are passed, "is_LequivalentR" defaults to "[0,1]"
621 and compares the first two lists passed to the function. So,
622
623 $eqv = is_LequivalentR( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
624
625 ... translates to:
626
627 $eqv = is_LequivalentR( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [0,1] );
628
629 · To determine whether any two of the lists passed to the function
630 are disjoint from one another (i.e., have no common members), pro‐
631 vide "is_LdisjointR()" with two array references. The first is a
632 reference to an array of array references, the arrays holding the
633 lists under consideration. The second of these is a reference to a
634 two-element array consisting of the two lists being tested for dis‐
635 jointedness. A true value (1) is returned if the lists are dis‐
636 joint; a false value (0) is returned otherwise.
637
638 Example: To determine whether @Don and @Ed are disjoint, call:
639
640 $disj = is_LdisjointR(
641 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
642 [3,4]
643 );
644
645 or
646
647 $disj = is_LdisjointR( {
648 items => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
649 pair => [3,4]
650 } );
651
652 · Pretty-print a chart showing the subset relationships among the
653 various source lists:
654
655 print_subset_chart( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
656
657 or
658
659 print_subset_chart( { lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] } );
660
661 · Pretty-print a chart showing the equivalence relationships among
662 the various source lists:
663
664 print_equivalence_chart( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
665
666 or
667
668 print_equivalence_chart( { lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] } );
669
670 · Determine in which (if any) of several lists a given string can be
671 found. Pass two array references, the first of which holds refer‐
672 ences to arrays holding the lists under consideration, and the sec‐
673 ond of which holds a single-item list consisting of the string
674 being tested.
675
676 @memb_arr = is_member_which(
677 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
678 [ 'abel' ]
679 );
680
681 or
682
683 @memb_arr = is_member_which( {
684 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
685 item => 'abel', # value is string
686 } );
687
688 In list context, return a list of those indices in the function's
689 argument list corresponding to lists holding the string being
690 tested. In the example above, @memb_arr will be:
691
692 ( 0 )
693
694 because 'abel' is found only in @Al which holds position 0 in the
695 list of arguments passed to "is_member_which()".
696
697 · As with other List::Compare::Functional functions which return a
698 list, you may wish the above function returned a reference to an
699 array holding the list:
700
701 $memb_arr_ref = is_member_which_ref(
702 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
703 [ 'jerky' ]
704 );
705
706 or
707
708 $memb_arr_ref = is_member_which_ref( {
709 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
710 item => 'jerky', # value is string
711 } );
712
713 In the example above, $memb_arr_ref will be:
714
715 [ 3, 4 ]
716
717 because 'jerky' is found in @Don and @Ed, which hold positions 3
718 and 4, respectively, in the list of arguments passed to "is_mem‐
719 ber_which()".
720
721 Note: functions "is_member_which()" and "is_member_which_ref" test
722 only one string at a time and hence take only one element in the
723 second array reference argument. To test more than one string at a
724 time see the next function, "are_members_which()".
725
726 · Determine in "which" (if any) of several lists one or more given
727 strings can be found. Pass two array references, the first of
728 which holds references to arrays holding the lists under considera‐
729 tion, and the second of which holds a list of the strings being
730 tested.
731
732 $memb_hash_ref = are_members_which(
733 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
734 [ qw⎪ abel baker fargo hilton zebra ⎪ ]
735 );
736
737 or
738
739 $memb_hash_ref = are_members_which( {
740 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
741 items => [ qw⎪ abel baker fargo hilton zebra ⎪ ], # value is arrayref
742 } );
743
744 The return valus is a reference to a hash of arrays. In this hash,
745 each element's value is a reference to an anonymous array whose
746 elements are those indices in the argument list corresponding to
747 lists holding the strings being tested. In the two examples above,
748 $memb_hash_ref will be:
749
750 {
751 abel => [ 0 ],
752 baker => [ 0, 1 ],
753 fargo => [ 0, 1, 2, 3, 4 ],
754 hilton => [ 1, 2 ],
755 zebra => [ ],
756 };
757
758 Note: "are_members_which()" tests more than one string at a time.
759 Hence, its second array reference argument can take more than one
760 element. "is_member_which()" and "is_member_which_ref()" each take
761 only one element in their second array reference arguments.
762 "are_members_which()" returns a hash reference; the other functions
763 return either a list or a reference to an array holding that list,
764 depending on context.
765
766 · Determine whether a given string can be found in any of several
767 lists. Pass two array references, the first of which holds refer‐
768 ences to arrays holding the lists under consideration, and the sec‐
769 ond of which holds a single-item list of the string being tested.
770
771 $found = is_member_any(
772 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
773 [ 'abel' ]
774 );
775
776 or
777
778 $found = is_member_any( {
779 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
780 item => 'abel', # value is string
781 } );
782
783 The return value is 1 if a specified string can be found in any of
784 the lists and 0 if not. In the example above, $found will be 1
785 because "abel" is found in one or more of the lists passed as argu‐
786 ments to "is_member_any()".
787
788 · Determine whether a specified string or strings can be found in any
789 of several lists. Pass two array references, the first of which
790 holds references to arrays holding the lists under consideration,
791 and the second of which holds a list of the strings being tested.
792
793 $memb_hash_ref = are_members_any(
794 [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
795 [ qw⎪ abel baker fargo hilton zebra ⎪ ]
796 );
797
798 or
799
800 $memb_hash_ref = are_members_any( {
801 lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
802 items => [ qw⎪ abel baker fargo hilton zebra ⎪ ], # value is arrayref
803 } );
804
805 The return value is a reference to a hash where an element's key is
806 the string being tested and the element's value is 1 if the string
807 can be found in any of the lists and 0 if not. In the example
808 above, $memb_hash_ref will be:
809
810 {
811 abel => 1,
812 baker => 1,
813 fargo => 1,
814 hilton => 1,
815 zebra => 0,
816 };
817
818 "zebra"'s value is 0 because "zebra" is not found in any of the
819 lists passed as arguments to "are_members_any()".
820
821 · Return current List::Compare::Functional version number:
822
823 $vers = get_version;
824
825 Comparing Lists Held in Seen-Hashes
826
827 What is a seen-hash? A seen-hash is a typical Perl implementation of a
828 look-up table: a hash where the value for a given element represents
829 the number of times the element's key is observed in a list. For the
830 purposes of List::Compare::Functional, what is crucial is whether an
831 item is observed in a list or not; how many times the item occurs in a
832 list is, with one exception, irrelevant. (That exception is the
833 "get_bag()" function and its fraternal twin "get_bag_ref()". In this
834 case only, the key in each element of the seen-hash is placed in the
835 bag the number of times indicated by the value of that element.) The
836 value of an element in a List::Compare seen-hash must be a positive
837 integer, but whether that integer is 1 or 1,000,001 is immaterial for
838 all List::Compare::Functional functions except forming a bag.
839
840 The two lists compared above were represented by arrays; references to
841 those arrays were passed to the various List::Compare::Functional func‐
842 tions. They could, however, have been represented by seen-hashes such
843 as the following and passed in exactly the same manner to the various
844 functions.
845
846 %Llist = (
847 abel => 2,
848 baker => 1,
849 camera => 1,
850 delta => 1,
851 edward => 1,
852 fargo => 1,
853 golfer => 1,
854 );
855 %Rlist = (
856 baker => 1,
857 camera => 1,
858 delta => 2,
859 edward => 1,
860 fargo => 1,
861 golfer => 1,
862 hilton => 1,
863 );
864
865 @intersection = get_intersection( [ \%Llist, \%Rlist ] );
866 @union = get_union( [ \%Llist, \%Rlist ] );
867 @complement = get_complement( [ \%Llist, \%Rlist ] );
868
869 and so forth.
870
871 To compare three or more lists simultaneously, provide the appropriate
872 List::Compare::Functional function with a first array reference holding
873 a list of three or more references to seen-hashes. Thus,
874
875 @union = get_intersection( [ \%Alpha, \%Beta, \%Gamma ] );
876
877 The 'single hashref' format for List::Compare::Functional functions is
878 also available when passing seen-hashes as arguments. Examples:
879
880 @intersection = get_intersection( {
881 lists => [ \%Alpha, \%Beta, \%Gamma ],
882 } );
883
884 @Ronly = get_complement( {
885 lists => [ \%Alpha, \%Beta, \%Gamma ],
886 item => 3,
887 } );
888
889 $LR = is_LsubsetR( {
890 lists => [ \%Alpha, \%Beta, \%Gamma ],
891 pair => [ 4, 2 ],
892 } );
893
894 $memb_hash_ref = are_members_any( {
895 lists => [ \%Alpha, \%Beta, \%Gamma ],
896 items => [ qw⎪ abel baker fargo hilton zebra ⎪ ],
897 } );
898
899 Faster Results with the Unsorted Option
900
901 By default, List::Compare::Function functions return lists sorted in
902 Perl's default ASCII-betical mode. Sorting entails a performance cost,
903 and if you do not need a sorted list and do not wish to pay this per‐
904 formance cost, you may call the following List::Compare::Function func‐
905 tions with the 'unsorted' option:
906
907 @intersection = get_intersection( '-u', [ \@Llist, \@Rlist ] );
908 @union = get_union( '-u', [ \@Llist, \@Rlist ] );
909 @Lonly = get_unique( '-u', [ \@Llist, \@Rlist ] );
910 @Ronly = get_complement( '-u', [ \@Llist, \@Rlist ] );
911 @LorRonly = get_symmetric_difference('-u', [ \@Llist, \@Rlist ] );
912 @bag = get_bag( '-u', [ \@Llist, \@Rlist ] );
913
914 For greater readability, the option may be spelled out:
915
916 @intersection = get_intersection('--unsorted', [ \@Llist, \@Rlist ] );
917
918 or
919
920 @intersection = get_intersection( {
921 unsorted => 1,
922 lists => [ \@Llist, \@Rlist ],
923 } );
924
925 Should you need a reference to an unsorted list as the return value,
926 you may call the unsorted option as follows:
927
928 $intersection_ref = get_intersection_ref(
929 '-u', [ \@Llist, \@Rlist ] );
930 $intersection_ref = get_intersection_ref(
931 '--unsorted', [ \@Llist, \@Rlist ] );
932
934 General Comments
935
936 List::Compare::Functional is a non-object-oriented implementation of
937 very common Perl code used to determine interesting relationships
938 between two or more lists at a time. List::Compare::Functional is
939 based on the same author's List::Compare module found in the same CPAN
940 distribution. List::Compare::Functional is closely modeled on the
941 ''Accelerated'' mode in List::Compare.
942
943 For a discussion of the antecedents of this module, see the discussion
944 of the history and development of this module in the documentation to
945 List::Compare.
946
947 List::Compare::Functional's Export Tag Groups
948
949 By default, List::Compare::Functional exports no functions. You may
950 import individual functions into your main package but may find it more
951 convenient to import via export tag groups. Four such groups are cur‐
952 rently defined:
953
954 use List::Compare::Functional qw(:main)
955 use List::Compare::Functional qw(:mainrefs)
956 use List::Compare::Functional qw(:originals)
957 use List::Compare::Functional qw(:aliases)
958
959 · Tag group ":main" includes what, in the author's opinion, are the
960 six List::Compare::Functional subroutines mostly likely to be used:
961
962 get_intersection()
963 get_union()
964 get_unique()
965 get_complement()
966 get_symmetric_difference()
967 is_LsubsetR()
968
969 · Tag group ":mainrefs" includes five of the six subroutines found in
970 ":main" -- all except "is_LsubsetR()" -- in the form in which they
971 return references to arrays rather than arrays proper:
972
973 get_intersection_ref()
974 get_union_ref()
975 get_unique_ref()
976 get_complement_ref()
977 get_symmetric_difference_ref()
978
979 · Tag group ":originals" includes all List::Compare::Functional sub‐
980 routines in their 'original' form, i.e., no aliases for those sub‐
981 routines:
982
983 get_intersection
984 get_intersection_ref
985 get_union
986 get_union_ref
987 get_unique
988 get_unique_ref
989 get_unique_all
990 get_complement
991 get_complement_ref
992 get_complement_all
993 get_symmetric_difference
994 get_symmetric_difference_ref
995 get_shared
996 get_shared_ref
997 get_nonintersection
998 get_nonintersection_ref
999 is_LsubsetR
1000 is_RsubsetL
1001 is_LequivalentR
1002 is_LdisjointR
1003 is_member_which
1004 is_member_which_ref
1005 are_members_which
1006 is_member_any
1007 are_members_any
1008 print_subset_chart
1009 print_equivalence_chart
1010 get_bag
1011 get_bag_ref
1012
1013 · Tag group ":aliases" contains all List::Compare::Functional subrou‐
1014 tines which are aliases for subroutines found in tag group ":origi‐
1015 nals". These are provided simply for less typing.
1016
1017 get_symdiff
1018 get_symdiff_ref
1019 is_LeqvlntR
1020
1021 April 2004 Change of Interface
1022
1023 Note: You can skip this section unless you used List::Compare::Func‐
1024 tional prior to the release of Version 0.25 in April 2004.
1025
1026 Version 0.25 initiated a significant change in the interface to this
1027 module's various functions. In order to be able to accommodate compar‐
1028 isons among more than two lists, it was necessary to change the type of
1029 arguments passed to the various functions. Whereas previously a typi‐
1030 cal List::Compare::Functional function would be called like this:
1031
1032 @intersection = get_intersection( \@Llist, \@Rlist ); # SUPERSEDED
1033
1034 ... now the references to the lists being compared must now be placed
1035 within a wrapper array (anonymous or named), a reference to which is
1036 now passed to the function, like so:
1037
1038 @intersection = get_intersection( [ \@Llist, \@Rlist ] );
1039
1040 ... or, alternatively:
1041
1042 @to_be_compared = (\@Llist, \@Rlist);
1043 @intersection = get_intersection( \@to_be_compared );
1044
1045 In a similar manner, List::Compare::Functional functions could previ‐
1046 ously take arguments in the form of references to 'seen-hashes' instead
1047 of references to arrays:
1048
1049 @intersection = get_intersection( \%h0, \%h1 );
1050
1051 (See above for discussion of seen-hashes.) Now, those references to
1052 seen-hashes must be placed within a wrapper array (anonymous or named),
1053 a reference to which is passed to the function, like so:
1054
1055 @intersection = get_intersection( [ \%h0, \%h1 ] );
1056
1057 Also, in a similar manner, some List::Compare::Functional functions
1058 previously took arguments in addition to the lists being compared.
1059 These arguments were simply passed as scalars, like this:
1060
1061 @memb_arr = is_member_which(\@Llist, \@Rlist, 'abel');
1062
1063 Now these arguments must also be placed within a wrapper array (anony‐
1064 mous or named), a reference to which is now passed to the function,
1065 like so:
1066
1067 @memb_arr = is_member_which( [ \@Llist, \@Rlist ], [ 'abel' ] );
1068
1069 ... or, alternatively:
1070
1071 @to_be_compared = (\@Llist, \@Rlist);
1072 @opts = ( 'abel' );
1073 @memb_arr = is_member_which( \@to_be_compared, \@opts );
1074
1075 As in previous versions, for a speed boost the user may provide the
1076 '-u' or '--unsorted' option as the first argument to some List::Com‐
1077 pare::Functional functions. Using this option, the "get_intersec‐
1078 tion()" function above would appear as:
1079
1080 @intersection = get_intersection( '-u', [ \@Llist, \@Rlist ] );
1081
1082 ... or, alternatively:
1083
1084 @intersection = get_intersection( '--unsorted', [ \@Llist, \@Rlist ] );
1085
1086 The arguments to any List::Compare::Functional function will therefore
1087 consist possibly of the unsorted option, and then of either one or two
1088 references to arrays, the first of which is a reference to an array of
1089 arrays or an array of seen-hashes.
1090
1092 James E. Keenan (jkeenan@cpan.org). When sending correspondence,
1093 please include 'List::Compare::Functional' or 'List-Compare-Functional'
1094 in your subject line.
1095
1096 Creation date: May 20, 2002. Last modification date: February 19,
1097 2006. Copyright (c) 2002-04 James E. Keenan. United States. All
1098 rights reserved. This is free software and may be distributed under
1099 the same terms as Perl itself.
1100
1101
1102
1103perl v5.8.8 2006-02-18 List::Compare::Functional(3)