1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
|
package List::Compare::Functional;
our $VERSION = '0.55';
our @ISA = qw(Exporter);
our @EXPORT_OK = qw|
get_intersection
get_intersection_ref
get_union
get_union_ref
get_unique
get_unique_ref
get_unique_all
get_complement
get_complement_ref
get_complement_all
get_symmetric_difference
get_symmetric_difference_ref
is_LsubsetR
is_RsubsetL
is_LequivalentR
is_LdisjointR
is_member_which
is_member_which_ref
are_members_which
is_member_any
are_members_any
print_subset_chart
print_equivalence_chart
get_shared
get_shared_ref
get_nonintersection
get_nonintersection_ref
get_symdiff
get_symdiff_ref
is_LeqvlntR
get_bag
get_bag_ref
get_version
|;
our %EXPORT_TAGS = (
main => [ qw(
get_intersection
get_union
get_unique
get_complement
get_symmetric_difference
is_LsubsetR
) ],
mainrefs => [ qw(
get_intersection_ref
get_union_ref
get_unique_ref
get_complement_ref
get_symmetric_difference_ref
) ],
originals => [ qw(
get_intersection
get_intersection_ref
get_union
get_union_ref
get_unique
get_unique_ref
get_unique_all
get_complement
get_complement_ref
get_complement_all
get_symmetric_difference
get_symmetric_difference_ref
get_shared
get_shared_ref
get_nonintersection
get_nonintersection_ref
is_LsubsetR
is_RsubsetL
is_LequivalentR
is_LdisjointR
is_member_which
is_member_which_ref
are_members_which
is_member_any
are_members_any
print_subset_chart
print_equivalence_chart
get_bag
get_bag_ref
get_version
) ],
aliases => [ qw(
get_symdiff
get_symdiff_ref
is_LeqvlntR
) ],
);
use strict;
local $^W = 1;
use Carp;
use List::Compare::Base::_Auxiliary qw(
_subset_subengine
_chart_engine_multiple
_equivalent_subengine
_calc_seen1
);
use List::Compare::Base::_Auxiliary qw(:calculate :checker :tester);
use List::Compare::Base::_Engine qw(
_unique_all_engine
_complement_all_engine
);
sub get_union {
return @{ get_union_ref(@_) };
}
sub get_union_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _union_engine(_argument_checker($argref))
: return [ sort @{_union_engine(_argument_checker($argref))} ];
}
sub _union_engine {
my $seenrefsref = _calc_seen1(@_);
my $unionhashref = _calculate_union_only($seenrefsref);
return [ keys %{$unionhashref} ];
}
sub get_intersection {
return @{ get_intersection_ref(@_) };
}
sub get_intersection_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _intersection_engine(_argument_checker($argref))
: return [ sort @{_intersection_engine(_argument_checker($argref))} ];
}
sub _intersection_engine {
my $seenrefsref = _calc_seen1(@_);
my @vals = sort { scalar(keys(%{$a})) <=> scalar(keys(%{$b})) }
@{$seenrefsref};
my %intersection = map { $_ => 1 } keys %{$vals[0]};
for my $l ( 1..$#vals ) {
%intersection = map { $_ => 1 }
grep { exists $intersection{$_} }
keys %{$vals[$l]};
}
return [ keys %intersection ];
}
sub get_unique {
return @{ get_unique_ref(@_) };
}
sub get_unique_ref {
my ($argref, $unsorted) = _alt_construct_tester_3(@_);
$unsorted
? return _unique_engine(_argument_checker_3($argref))
: return [ sort @{_unique_engine(_argument_checker_3($argref))} ];
}
sub get_unique_all {
my ($argref, $unsorted) = _alt_construct_tester_3(@_);
# currently it doesn't appear that &_unique_all_engine can make use of
# $unsorted
return _unique_all_engine(_argument_checker_3a($argref));
}
sub _unique_engine {
my $index = pop(@_);
my $seenref = _calculate_seen_only(_calc_seen1(@_));
my %seen_in_all_others = ();
my @seenthis = ();
for my $i (keys %{$seenref}) {
unless ($i == $index) {
for my $k (keys %{$seenref->{$i}}) {
$seen_in_all_others{$k}++;
}
}
else {
@seenthis = keys %{$seenref->{$index}};
}
}
my @unique_to_this_index = ();
for my $s (@seenthis) {
push @unique_to_this_index, $s
unless $seen_in_all_others{$s};
}
return \@unique_to_this_index;
}
sub get_complement {
return @{ get_complement_ref(@_) };
}
sub get_complement_ref {
my ($argref, $unsorted) = _alt_construct_tester_3(@_);
$unsorted
? return _complement_engine(_argument_checker_3($argref))
: return [ sort @{_complement_engine(_argument_checker_3($argref))} ];
}
sub get_complement_all {
my ($argref, $unsorted) = _alt_construct_tester_3(@_);
return _complement_all_engine(_argument_checker_3a($argref), $unsorted);
}
sub _complement_engine {
my $tested = pop(@_);
my $seenrefsref = _calc_seen1(@_);
my ($unionref, $seenref) = _calculate_union_seen_only($seenrefsref);
# Calculate %xcomplement
# Inputs: $seenrefsref @union (keys %$unionref)
my (%xcomplement);
for (my $i = 0; $i <= $#{$seenrefsref}; $i++) {
my @complementthis = ();
foreach my $k (keys %{$unionref}) {
push(@complementthis, $k) unless (exists $seenref->{$i}->{$k});
}
$xcomplement{$i} = \@complementthis;
}
return [ @{$xcomplement{$tested}} ];
}
sub get_symmetric_difference {
return @{ get_symmetric_difference_ref(@_) };
}
sub get_symmetric_difference_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _symmetric_difference_engine(_argument_checker($argref))
: return [ sort @{_symmetric_difference_engine(_argument_checker($argref))} ];
}
sub _symmetric_difference_engine {
# Get those items which do not appear in more than one of several lists (their symmetric_difference);
my $seenrefsref = _calc_seen1(@_);
my $unionref = _calculate_union_only($seenrefsref);
my $sharedref = _calculate_sharedref($seenrefsref);
my (@symmetric_difference);
for my $k (keys %{$unionref}) {
push(@symmetric_difference, $k) unless exists $sharedref->{$k};
}
return \@symmetric_difference;
}
{
no warnings 'once';
*get_symdiff = \&get_symmetric_difference;
*get_symdiff_ref = \&get_symmetric_difference_ref;
}
sub get_shared {
return @{ get_shared_ref(@_) };
}
sub get_shared_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _shared_engine(_argument_checker($argref))
: return [ sort @{_shared_engine(_argument_checker($argref))} ];
}
sub _shared_engine {
my $seenrefsref = _calc_seen1(@_);
my $sharedref = _calculate_sharedref($seenrefsref);
return [ keys %{$sharedref} ];
}
sub get_nonintersection {
return @{ get_nonintersection_ref(@_) };
}
sub get_nonintersection_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _nonintersection_engine(_argument_checker($argref))
: return [ sort @{_nonintersection_engine(_argument_checker($argref))} ];
}
sub _nonintersection_engine {
my $seenrefsref = _calc_seen1(@_);
my $unionref =
_calculate_union_only($seenrefsref);
my @vals = sort { scalar(keys(%{$a})) <=> scalar(keys(%{$b})) }
@{$seenrefsref};
my %intersection = map { $_ => 1 } keys %{$vals[0]};
for my $l ( 1..$#vals ) {
%intersection = map { $_ => 1 }
grep { exists $intersection{$_} }
keys %{$vals[$l]};
}
# Calculate nonintersection
# Inputs: @union (keys %$unionref) %intersection
my (@nonintersection);
for my $k (keys %{$unionref}) {
push(@nonintersection, $k) unless exists $intersection{$k};
}
return \@nonintersection;
}
sub is_LsubsetR {
my $argref = _alt_construct_tester_4(@_);
return _is_LsubsetR_engine(_argument_checker_4($argref));
}
sub _is_LsubsetR_engine {
my $testedref = pop(@_);
my $xsubsetref = _subset_engine(@_);
return ${$xsubsetref}[${$testedref}[0]][${$testedref}[1]];
}
sub is_RsubsetL {
my $argref = _alt_construct_tester_4(@_);
return _is_RsubsetL_engine(_argument_checker_4($argref));
}
sub _is_RsubsetL_engine {
my $testedref = pop(@_);
my $xsubsetref = _subset_engine(@_);
return ${$xsubsetref}[${$testedref}[1]][${$testedref}[0]];
}
sub _subset_engine {
my $seenrefsref = _calc_seen1(@_);
my $xsubsetref = _subset_subengine($seenrefsref);
return $xsubsetref;
}
sub is_LequivalentR {
my $argref = _alt_construct_tester_4(@_);
return _is_LequivalentR_engine(_argument_checker_4($argref));
}
{ no warnings 'once'; *is_LeqvlntR = \&is_LequivalentR; }
sub _is_LequivalentR_engine {
my $testedref = pop(@_);
my $seenrefsref = _calc_seen1(@_);
my $xequivalentref = _equivalent_subengine($seenrefsref);
return ${$xequivalentref}[${$testedref}[1]][${$testedref}[0]];
}
sub is_LdisjointR {
my $argref = _alt_construct_tester_4(@_);
return _is_LdisjointR_engine(_argument_checker_4($argref));
}
sub _is_LdisjointR_engine {
my $testedref = pop(@_);
my $seenrefsref = _calc_seen1(@_);
my $disjoint = 1; # start out assuming disjoint status
OUTER: for my $k (keys %{$seenrefsref->[$testedref->[0]]}) {
if ($seenrefsref->[$testedref->[1]]->{$k}) {
$disjoint = 0;
last OUTER;
}
}
return $disjoint;
}
sub print_subset_chart {
my $argref = _alt_construct_tester_5(@_);
_print_subset_chart_engine(_argument_checker($argref));
}
sub _print_subset_chart_engine {
my $seenrefsref = _calc_seen1(@_);
my $xsubsetref = _subset_subengine($seenrefsref);
my $title = 'Subset';
_chart_engine_multiple($xsubsetref, $title);
}
sub print_equivalence_chart {
my $argref = _alt_construct_tester_5(@_);
_print_equivalence_chart_engine(_argument_checker($argref));
}
sub _print_equivalence_chart_engine {
my $seenrefsref = _calc_seen1(@_);
my $xequivalentref = _equivalent_subengine($seenrefsref);
my $title = 'Equivalence';
_chart_engine_multiple($xequivalentref, $title);
}
sub is_member_which {
return @{ is_member_which_ref(@_) };
}
sub is_member_which_ref {
my $argref = _alt_construct_tester_1(@_);
return _is_member_which_engine(_argument_checker_1($argref));
}
sub _is_member_which_engine {
my $arg = pop(@_);
my $seenrefsref = _calc_seen1(@_);
my $seenref = _calculate_seen_only($seenrefsref);
my (@found);
foreach (sort keys %{$seenref}) {
push @found, $_ if (exists ${$seenref}{$_}{$arg});
}
return \@found;
}
sub is_member_any {
my $argref = _alt_construct_tester_1(@_);
return _is_member_any_engine(_argument_checker_1($argref));
}
sub _is_member_any_engine {
my $tested = pop(@_);
my $seenrefsref = _calc_seen1(@_);
my $seenref = _calculate_seen_only($seenrefsref);
my ($k);
while ( $k = each %{$seenref} ) {
return 1 if (defined ${$seenref}{$k}{$tested});
}
return 0;
}
sub are_members_which {
my $argref = _alt_construct_tester_2(@_);
return _are_members_which_engine(_argument_checker_2($argref));
}
sub _are_members_which_engine {
my $testedref = pop(@_);
my @tested = @{$testedref};
my $seenrefsref = _calc_seen1(@_);
my $seenref = _calculate_seen_only($seenrefsref);
my (%found);
for (my $i=0; $i<=$#tested; $i++) {
my (@not_found);
foreach (sort keys %{$seenref}) {
exists ${${$seenref}{$_}}{$tested[$i]}
? push @{$found{$tested[$i]}}, $_
: push @not_found, $_;
}
$found{$tested[$i]} = [] if (@not_found == keys %{$seenref});
}
return \%found;
}
sub are_members_any {
my $argref = _alt_construct_tester_2(@_);
return _are_members_any_engine(_argument_checker_2($argref));
}
sub _are_members_any_engine {
my $testedref = pop(@_);
my @tested = @{$testedref};
my $seenrefsref = _calc_seen1(@_);
my $seenref = _calculate_seen_only($seenrefsref);
my (%present);
for (my $i=0; $i<=$#tested; $i++) {
foreach (keys %{$seenref}) {
unless (defined $present{$tested[$i]}) {
$present{$tested[$i]} = 1 if ${$seenref}{$_}{$tested[$i]};
}
}
$present{$tested[$i]} = 0 if (! defined $present{$tested[$i]});
}
return \%present;
}
sub get_bag {
return @{ get_bag_ref(@_) };
}
sub get_bag_ref {
my ($argref, $unsorted) = _alt_construct_tester(@_);
$unsorted
? return _bag_engine(_argument_checker($argref))
: return [ sort @{_bag_engine(_argument_checker($argref))} ];
}
sub _bag_engine {
my @listrefs = @_;
my (@bag);
if (ref($listrefs[0]) eq 'ARRAY') {
foreach my $lref (@listrefs) {
foreach my $el (@{$lref}) {
push(@bag, $el);
}
}
} else {
foreach my $lref (@listrefs) {
foreach my $key (keys %{$lref}) {
for (my $j=1; $j <= ${$lref}{$key}; $j++) {
push(@bag, $key);
}
}
}
}
return \@bag;
}
sub get_version {
return $List::Compare::Functional::VERSION;
}
1;
__END__
=head1 NAME
List::Compare::Functional - Compare elements of two or more lists
=head1 VERSION
This document refers to version 0.55 of List::Compare::Functional.
This version was released August 16 2020. The first released
version of List::Compare::Functional was v0.21. Its version numbers
are set to be consistent with the other parts of the List::Compare
distribution.
=head2 Notice of Interface Changes
Certain significant changes to the interface to List::Compare::Functional
were made with the introduction of Version 0.25 in April 2004. The
documentation immediately below reflects those changes, so if you are
first using this module with that or a later version, simply read and
follow the documentation below. If, however, you used List::Compare::Functional
prior to that version, see the discussion of interface changes farther
below: April 2004 Change of Interface.
=head1 SYNOPSIS
=head2 Getting Started
List::Compare::Functional exports no subroutines by default.
use List::Compare::Functional qw(:originals :aliases);
will import all publicly available subroutines from
List::Compare::Functional. The model for importing just one subroutine from
List::Compare::Functional is:
use List::Compare::Functional qw( get_intersection );
It will probably be most convenient for the user to import functions by
using one of the two following export tags:
use List::Compare::Functional qw(:main :mainrefs);
The assignment of the various comparison functions to export tags is
discussed below.
For clarity, we shall begin by discussing comparisons of just two lists at
a time. Farther below, we shall discuss comparisons among three or more
lists at a time.
=head2 Comparing Two Lists Held in Arrays
=over 4
=item *
Given two lists:
@Llist = qw(abel abel baker camera delta edward fargo golfer);
@Rlist = qw(baker camera delta delta edward fargo golfer hilton);
=item *
Get those items which appear at least once in both lists (their intersection).
@intersection = get_intersection( [ \@Llist, \@Rlist ] );
Note that you could place the references to the lists being compared into
a named array and then pass C<get_intersection()> a reference to that array.
@to_be_compared = ( \@Llist, \@Rlist );
@intersection = get_intersection( \@to_be_compared );
Beginning with version 0.29 (May 2004), List::Compare::Functional now offers
an additional way of passing arguments to its various functions. If you
prefer to see a more explicit delineation among the types of arguments passed
to a function, pass a single hash reference which holds the lists being
compared in an anonymous array which is the value corresponding to key C<lists>:
@intersection = get_intersection( {
lists => [ \@Llist, \@Rlist ],
} );
=item *
Get those items which appear at least once in either list (their union).
@union = get_union( [ \@Llist, \@Rlist ] );
or
@union = get_union( { lists => [ \@Llist, \@Rlist ] } );
=item *
Get those items which appear (at least once) only in the first list.
@Lonly = get_unique( [ \@Llist, \@Rlist ] );
or
@Lonly = get_unique( { lists => [ \@Llist, \@Rlist ] } );
=item *
Get those items which appear (at least once) only in the second list.
@Ronly = get_complement( [ \@Llist, \@Rlist ] );
or
@Ronly = get_complement( { lists => [ \@Llist, \@Rlist ] } );
=item *
@LorRonly = get_symmetric_difference( [ \@Llist, \@Rlist ] );
@LorRonly = get_symdiff( [ \@Llist, \@Rlist ] ); # alias
or
@LorRonly = get_symmetric_difference( { lists => [ \@Llist, \@Rlist ] } );
=item *
Make a bag of all those items in both lists. The bag differs from the
union of the two lists in that it holds as many copies of individual
elements as appear in the original lists.
@bag = get_bag( [ \@Llist, \@Rlist ] );
or
@bag = get_bag( { lists => [ \@Llist, \@Rlist ] } );
=item *
An alternative approach to the above functions: If you do not immediately
require an array as the return value of the function call, but simply need
a I<reference> to an (anonymous) array, use one of the following
parallel functions:
$intersection_ref = get_intersection_ref( [ \@Llist, \@Rlist ] );
$union_ref = get_union_ref( [ \@Llist, \@Rlist ] );
$Lonly_ref = get_unique_ref( [ \@Llist, \@Rlist ] );
$Ronly_ref = get_complement_ref( [ \@Llist, \@Rlist ] );
$LorRonly_ref = get_symmetric_difference_ref( [ \@Llist, \@Rlist ] );
$LorRonly_ref = get_symdiff_ref( [ \@Llist, \@Rlist ] );
# alias
$bag_ref = get_bag_ref( [ \@Llist, \@Rlist ] );
or
$intersection_ref =
get_intersection_ref( { lists => [ \@Llist, \@Rlist ] } );
$union_ref =
get_union_ref( { lists => [ \@Llist, \@Rlist ] } );
$Lonly_ref =
get_unique_ref( { lists => [ \@Llist, \@Rlist ] } );
$Ronly_ref =
get_complement_ref( { lists => [ \@Llist, \@Rlist ] } );
$LorRonly_ref =
get_symmetric_difference_ref( { lists => [ \@Llist, \@Rlist ] } );
$LorRonly_ref =
get_symdiff_ref( { lists => [ \@Llist, \@Rlist ] } );
# alias
$bag_ref =
get_bag_ref( { lists => [ \@Llist, \@Rlist ] } );
=item *
Return a true value if the first list ('L' for 'left') is a subset of the
second list ('R' for 'right').
$LR = is_LsubsetR( [ \@Llist, \@Rlist ] );
or
$LR = is_LsubsetR( { lists => [ \@Llist, \@Rlist ] } );
=item *
Return a true value if R is a subset of L.
$RL = is_RsubsetL( [ \@Llist, \@Rlist ] );
or
$RL = is_RsubsetL( { lists => [ \@Llist, \@Rlist ] } );
=item *
Return a true value if L and R are equivalent, I<i.e.,> if every element
in L appears at least once in R and I<vice versa>.
$eqv = is_LequivalentR( [ \@Llist, \@Rlist ] );
$eqv = is_LeqvlntR( [ \@Llist, \@Rlist ] ); # alias
or
$eqv = is_LequivalentR( { lists => [ \@Llist, \@Rlist ] } );
=item *
Return a true value if L and R are disjoint, I<i.e.,> if L and R have
no common elements.
$disj = is_LdisjointR( [ \@Llist, \@Rlist ] );
or
$disj = is_LdisjointR( { lists => [ \@Llist, \@Rlist ] } );
=item *
Pretty-print a chart showing whether one list is a subset of the other.
print_subset_chart( [ \@Llist, \@Rlist ] );
or
print_subset_chart( { lists => [ \@Llist, \@Rlist ] } );
=item *
Pretty-print a chart showing whether the two lists are equivalent (same
elements found at least once in both).
print_equivalence_chart( [ \@Llist, \@Rlist ] );
or
print_equivalence_chart( { lists => [ \@Llist, \@Rlist ] } );
=item *
Determine in I<which> (if any) of the lists a given string can be found.
In list context, return a list of those indices in the argument list
corresponding to lists holding the string being tested.
@memb_arr = is_member_which( [ \@Llist, \@Rlist ] , [ 'abel' ] );
or
@memb_arr = is_member_which( {
lists => [ \@Llist, \@Rlist ], # value is array reference
item => 'abel', # value is string
} );
In the example above, C<@memb_arr> will be:
( 0 )
because C<'abel'> is found only in C<@Al> which holds position C<0> in the
list of arguments passed to C<new()>.
=item *
As with other List::Compare::Functional functions which return a list, you
may wish the above function returned a (scalar) reference to an array
holding the list:
$memb_arr_ref = is_member_which_ref( [ \@Llist, \@Rlist ] , [ 'baker' ] );
or
$memb_arr_ref = is_member_which_ref( {
lists => [ \@Llist, \@Rlist ], # value is array reference
item => 'baker', # value is string
} );
In the example above, C<$memb_arr_ref> will be:
[ 0, 1 ]
because C<'baker'> is found in C<@Llist> and C<@Rlist>, which hold positions
C<0> and C<1>, respectively, in the list of arguments passed to C<new()>.
B<Note:> functions C<is_member_which()> and C<is_member_which_ref> test
only one string at a time and hence take only one argument. To test more
than one string at a time see the next function, C<are_members_which()>.
=item *
Determine in C<which> (if any) of the lists passed as arguments one or
more given strings can be found. The lists beings searched are placed in an
array, a reference to which is the first argument passed to
C<are_members_which()>. The strings to be tested are also placed in an
array, a reference to which is the second argument passed to that function.
$memb_hash_ref =
are_members_which( [ \@Llist, \@Rlist ] ,
[ qw| abel baker fargo hilton zebra | ]
);
or
$memb_hash_ref = are_members_which( {
lists => [ \@Llist, \@Rlist ], # value is arrayref
items => [ qw| abel baker fargo hilton zebra | ], # value is arrayref
} );
The return value is a reference to a hash of arrays. The
key for each element in this hash is the string being tested. Each element's
value is a reference to an anonymous array whose elements are those indices in
the constructor's argument list corresponding to lists holding the strings
being tested. In the examples above, C<$memb_hash_ref> will be:
{
abel => [ 0 ],
baker => [ 0, 1 ],
fargo => [ 0, 1 ],
hilton => [ 1 ],
zebra => [ ],
};
B<Note:> C<are_members_which()> can take more than one argument;
C<is_member_which()> and C<is_member_which_ref()> each take only one argument.
Unlike those functions, C<are_members_which()> returns a hash reference.
=item *
Determine whether a given string can be found in I<any> of the lists passed as
arguments. Return C<1> if a specified string can be found in any of the lists
and C<0> if not.
$found = is_member_any( [ \@Llist, \@Rlist ] , [ 'abel' ] );
or
$found = is_member_any( {
lists => [ \@Llist, \@Rlist ], # value is array reference
item => 'abel', # value is string
} );
In the example above, C<$found> will be C<1> because C<'abel'> is found in one
or more of the lists passed as arguments to C<new()>.
=item *
Determine whether a specified string or strings can be found in I<any> of the
lists passed as arguments. The lists beings searched are placed in an
array, a reference to which is the first argument passed to
C<are_members_any()>. The strings to be tested are also placed in an
array, a reference to which is the second argument passed to that function.
$memb_hash_ref =
are_members_any( [ \@Llist, \@Rlist ] ,
[ qw| abel baker fargo hilton zebra | ]
);
or
$memb_hash_ref = are_members_any( {
lists => [ \@Llist, \@Rlist ], # value is arrayref
items => [ qw| abel baker fargo hilton zebra | ], # value is arrayref
} );
The return value is a reference to a hash where an element's key is the
string being tested and the element's value is C<1> if the string can be
found in I<any> of the lists and C<0> if not. In the examples above,
C<$memb_hash_ref> will be:
{
abel => 1,
baker => 1,
fargo => 1,
hilton => 1,
zebra => 0,
};
C<zebra>'s value is C<0> because C<zebra> is not found in either of the lists
passed as arguments to C<are_members_any()>.
=item *
Return current List::Compare::Functional version number.
$vers = get_version;
=back
=head2 Comparing Three or More Lists Held in Arrays
Given five lists:
@Al = qw(abel abel baker camera delta edward fargo golfer);
@Bob = qw(baker camera delta delta edward fargo golfer hilton);
@Carmen = qw(fargo golfer hilton icon icon jerky kappa);
@Don = qw(fargo icon jerky);
@Ed = qw(fargo icon icon jerky);
=over 4
=item *
Get those items which appear at least once in I<each> list (their intersection).
@intersection = get_intersection( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
@intersection = get_intersection( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
Get those items which appear at least once in I<any> of the lists (their union).
@union = get_union( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
@union = get_union( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
To get those items which are unique to a particular list, provide C<get_unique()>
with two array references. The first holds references to the arrays
which in turn hold the individual lists being compared. The second holds
the index position in the first reference of the particular list under
consideration. Example: To get elements unique to C<@Carmen>:
@Lonly = get_unique(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 2 ]
);
or
@Lonly = get_unique( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
item => 2, # value is number
} );
If no index position is passed to C<get_unique()> it will default to C<0>
and report items unique to the first list passed to the function. Hence,
@Lonly = get_unique( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
is same as:
@Lonly = get_unique( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [ 0 ] );
=item *
Should you need to identify the items unique to I<each> of the lists under
consideration, call C<get_unique_all> and get a reference to an array of
array references:
$unique_all_ref = get_unique_all(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ]
);
or
$unique_all_ref = get_unique_all( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
To get those items which appear only in lists I<other than> one particular
list, pass two array references to the C<get_complement()> function.
The first holds references to the arrays which in turn hold the individual lists
being compared. The second holds the index position in the first reference
of the particular list under consideration. Example: to get all the
elements found in lists other than C<@Don>:
@Ronly = get_complement(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 3 ]
);
or
@Ronly = get_complement( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
item => 3, # value is number
} );
If no index position is passed to C<get_complement()> it will default to C<0>
and report items found in all lists I<other than> the first list passed to
C<get_complement()>.
@Lonly = get_complement( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
is same as:
@Lonly = get_complement( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [ 0 ] );
=item *
Should you need to identify the items not found in I<each> of the lists under
consideration, call C<get_complement_all> and get a reference to an array of
array references:
$complement_all_ref = get_complement_all(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ]
);
or
$complement_all_ref = get_complement_all( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
Get those items which do I<not> appear in I<more than one> of several lists
(their symmetric_difference);
@LorRonly = get_symmetric_difference( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
@LorRonly = get_symdiff( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] ); # alias
or
@LorRonly = get_symmetric_difference( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
Get those items found in I<any> of several lists which do I<not> appear
in C<all> of the lists (I<i.e.,> all items except those found in the
intersection of the lists):
@nonintersection = get_nonintersection(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
@nonintersection = get_nonintersection( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
Get those items which appear in I<more than one> of several lists
(I<i.e.,> all items except those found in their symmetric difference);
@shared = get_shared( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
@shared = get_shared( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
Make a bag of every item found in every list. The bag differs from the
union of the two lists in that it holds as many copies of individual
elements as appear in the original lists.
@bag = get_bag( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
@bag = get_bag( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
} );
=item *
An alternative approach to the above functions: If you do not immediately
require an array as the return value of the function, but simply need
a I<reference> to an array, use one of the following parallel functions:
$intersection_ref = get_intersection_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$union_ref = get_union_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$Lonly_ref = get_unique_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$Ronly_ref = get_complement_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$LorRonly_ref = get_symmetric_difference_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$LorRonly_ref = get_symdiff_ref( # alias
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$nonintersection_ref = get_nonintersection_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$shared_ref = get_shared_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
$bag_ref = get_bag_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
=item *
To determine whether one particular list is a subset of another of the
lists passed to the function, pass to C<is_LsubsetR()> two array references.
The first of these is a reference to an array of array
references, the arrays holding the lists under consideration. The
second is a reference to a two-element array consisting of the
index of the presumed subset, followed by the index position of the presumed
superset. A true value (C<1>) is returned if the first (left-hand) element
in the second reference list is a subset of the second (right-hand) element;
a false value (C<0>) is returned otherwise.
Example: To determine whether C<@Ed> is a subset of C<@Carmen>, call:
$LR = is_LsubsetR(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 4, 2 ]
);
or
$LR = is_LsubsetR( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
pair => [ 4, 2 ], # value is arrayref
} );
If only the first reference (to the array of lists) is passed to
C<is_LsubsetR>, then the function's second argument defaults to C<(0,1)> and
compares the first two lists passed to the constructor. So,
$LR = is_LsubsetR([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
... is equivalent to:
$LR = is_LsubsetR([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [0,1] );
=item *
To reverse the order in which the particular lists are evaluated for
superset/subset status, call C<is_RsubsetL>:
$RL = is_RsubsetL([ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [2,4] );
or
$RL = is_RsubsetL( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
pair => [ 2, 4 ],
} );
=item *
List::Compare::Functional considers two lists to be equivalent if
every element in one list appears at least once in R and I<vice versa>.
To determine whether one particular list passed to the function is
equivalent to another of the lists passed to the function, provide
C<is_LequivalentR()> with two array references.
The first is a reference to an array of array
references, the arrays holding the lists under consideration. The
second of these is a reference to a two-element array consisting of the
two lists being tested for equivalence. A true value (C<1>) is returned if
the lists are equivalent; a false value (C<0>) is returned otherwise.
Example: To determine whether C<@Don> and C<@Ed> are equivalent, call:
$eqv = is_LequivalentR(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[3,4]
);
$eqv = is_LeqvlntR( # alias
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[3,4]
);
or
$eqv = is_LequivalentR( {
items => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
pair => [3,4],
} );
If no arguments are passed, C<is_LequivalentR> defaults to C<[0,1]> and
compares the first two lists passed to the function. So,
$eqv = is_LequivalentR( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
... translates to:
$eqv = is_LequivalentR( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], [0,1] );
=item *
To determine whether any two of the lists passed to the function are
disjoint from one another (I<i.e.,> have no common members), provide
C<is_LdisjointR()> with two array references.
The first is a reference to an array of array
references, the arrays holding the lists under consideration. The
second of these is a reference to a two-element array consisting of the
two lists being tested for disjointedness. A true value (C<1>) is returned if
the lists are disjoint; a false value (C<0>) is returned otherwise.
Example: To determine whether C<@Don> and C<@Ed> are disjoint, call:
$disj = is_LdisjointR(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[3,4]
);
or
$disj = is_LdisjointR( {
items => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
pair => [3,4]
} );
=item *
Pretty-print a chart showing the subset relationships among the various
source lists:
print_subset_chart( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
print_subset_chart( { lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] } );
=item *
Pretty-print a chart showing the equivalence relationships among the
various source lists:
print_equivalence_chart( [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] );
or
print_equivalence_chart( { lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ] } );
=item *
Determine in I<which> (if any) of several lists a given string can be found.
Pass two array references, the first of which holds references to arrays
holding the lists under consideration, and the second of which holds a
single-item list consisting of the string being tested.
@memb_arr = is_member_which(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 'abel' ]
);
or
@memb_arr = is_member_which( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
item => 'abel', # value is string
} );
In list context, return a list of those indices in the function's
argument list corresponding to lists holding the string being tested.
In the example above, C<@memb_arr> will be:
( 0 )
because C<'abel'> is found only in C<@Al> which holds position C<0> in the
list of arguments passed to C<is_member_which()>.
=item *
As with other List::Compare::Functional functions which return a list, you may
wish the above function returned a reference to an array holding the list:
$memb_arr_ref = is_member_which_ref(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 'jerky' ]
);
or
$memb_arr_ref = is_member_which_ref( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
item => 'jerky', # value is string
} );
In the example above, C<$memb_arr_ref> will be:
[ 3, 4 ]
because C<'jerky'> is found in C<@Don> and C<@Ed>, which hold positions
C<3> and C<4>, respectively, in the list of arguments passed to
C<is_member_which()>.
B<Note:> functions C<is_member_which()> and C<is_member_which_ref> test
only one string at a time and hence take only one element in the second
array reference argument. To test more than one string at a time see
the next function, C<are_members_which()>.
=item *
Determine in C<which> (if any) of several lists one or more given strings
can be found. Pass two array references, the first of which holds references
to arrays holding the lists under consideration, and the second of which
holds a list of the strings being tested.
$memb_hash_ref = are_members_which(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ qw| abel baker fargo hilton zebra | ]
);
or
$memb_hash_ref = are_members_which( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
items => [ qw| abel baker fargo hilton zebra | ], # value is arrayref
} );
The return valus is a reference to a hash of arrays. In this hash,
each element's value is a reference to an anonymous array whose
elements are those indices in the argument list corresponding to
lists holding the strings being tested. In the two examples above,
C<$memb_hash_ref> will be:
{
abel => [ 0 ],
baker => [ 0, 1 ],
fargo => [ 0, 1, 2, 3, 4 ],
hilton => [ 1, 2 ],
zebra => [ ],
};
B<Note:> C<are_members_which()> tests more than one string at a time. Hence,
its second array reference argument can take more than one element.
C<is_member_which()> and C<is_member_which_ref()> each take only one element
in their second array reference arguments. C<are_members_which()> returns a
hash reference; the other functions return either a list or a reference to an
array holding that list, depending on context.
=item *
Determine whether a given string can be found in I<any> of several lists.
Pass two array references, the first of which holds references
to arrays holding the lists under consideration, and the second of which
holds a single-item list of the string being tested.
$found = is_member_any(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ 'abel' ]
);
or
$found = is_member_any( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
item => 'abel', # value is string
} );
The return value is C<1> if a specified string can be found in I<any> of
the lists and C<0> if not. In the example above, C<$found> will be
C<1> because C<abel> is found in one or more of the lists passed as
arguments to C<is_member_any()>.
=item *
Determine whether a specified string or strings can be found in I<any> of
several lists. Pass two array references, the first of which holds references
to arrays holding the lists under consideration, and the second of which
holds a list of the strings being tested.
$memb_hash_ref = are_members_any(
[ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ],
[ qw| abel baker fargo hilton zebra | ]
);
or
$memb_hash_ref = are_members_any( {
lists => [ \@Al, \@Bob, \@Carmen, \@Don, \@Ed ], # value is arrayref
items => [ qw| abel baker fargo hilton zebra | ], # value is arrayref
} );
The return value is a reference to a hash where an element's key is the
string being tested and the element's value is C<1> if the string can be
found in any of the lists and C<0> if not. In the example above,
C<$memb_hash_ref> will be:
{
abel => 1,
baker => 1,
fargo => 1,
hilton => 1,
zebra => 0,
};
C<zebra>'s value is C<0> because C<zebra> is not found in any of the lists
passed as arguments to C<are_members_any()>.
=item *
Return current List::Compare::Functional version number:
$vers = get_version;
=back
=head2 Comparing Lists Held in Seen-Hashes
What is a seen-hash? A seen-hash is a typical Perl implementation of a
look-up table: a hash where the value for a given element represents the number
of times the element's key is observed in a list. For the purposes of
List::Compare::Functional, what is crucial is whether an item is observed in a
list or not; how many times the item occurs in a list is, I<with one exception,>
irrelevant. (That exception is the C<get_bag()> function and its fraternal
twin C<get_bag_ref()>. In this case only, the key in each element of the
seen-hash is placed in the bag the number of times indicated by the value of
that element.) The value of an element in a List::Compare seen-hash must be
a positive integer, but whether that integer is 1 or 1,000,001 is immaterial for
all List::Compare::Functional functions I<except> forming a bag.
The two lists compared above were represented by arrays; references to
those arrays were passed to the various List::Compare::Functional functions.
They could, however, have been represented by seen-hashes such as the following
and passed in exactly the same manner to the various functions.
%Llist = (
abel => 2,
baker => 1,
camera => 1,
delta => 1,
edward => 1,
fargo => 1,
golfer => 1,
);
%Rlist = (
baker => 1,
camera => 1,
delta => 2,
edward => 1,
fargo => 1,
golfer => 1,
hilton => 1,
);
@intersection = get_intersection( [ \%Llist, \%Rlist ] );
@union = get_union( [ \%Llist, \%Rlist ] );
@complement = get_complement( [ \%Llist, \%Rlist ] );
and so forth.
To compare three or more lists simultaneously, provide the appropriate
List::Compare::Functional function with a first array reference holding a
list of three or more references to seen-hashes. Thus,
@union = get_intersection( [ \%Alpha, \%Beta, \%Gamma ] );
The 'single hashref' format for List::Compare::Functional functions is
also available when passing seen-hashes as arguments. Examples:
@intersection = get_intersection( {
lists => [ \%Alpha, \%Beta, \%Gamma ],
} );
@Ronly = get_complement( {
lists => [ \%Alpha, \%Beta, \%Gamma ],
item => 3,
} );
$LR = is_LsubsetR( {
lists => [ \%Alpha, \%Beta, \%Gamma ],
pair => [ 4, 2 ],
} );
$memb_hash_ref = are_members_any( {
lists => [ \%Alpha, \%Beta, \%Gamma ],
items => [ qw| abel baker fargo hilton zebra | ],
} );
=head2 Faster Results with the Unsorted Option
By default, List::Compare::Function functions return lists sorted in Perl's
default ASCII-betical mode. Sorting entails a performance cost, and if you
do not need a sorted list and do not wish to pay this performance cost, you
may call the following List::Compare::Function functions with the 'unsorted'
option:
@intersection = get_intersection( '-u', [ \@Llist, \@Rlist ] );
@union = get_union( '-u', [ \@Llist, \@Rlist ] );
@Lonly = get_unique( '-u', [ \@Llist, \@Rlist ] );
@Ronly = get_complement( '-u', [ \@Llist, \@Rlist ] );
@LorRonly = get_symmetric_difference('-u', [ \@Llist, \@Rlist ] );
@bag = get_bag( '-u', [ \@Llist, \@Rlist ] );
For greater readability, the option may be spelled out:
@intersection = get_intersection('--unsorted', [ \@Llist, \@Rlist ] );
or
@intersection = get_intersection( {
unsorted => 1,
lists => [ \@Llist, \@Rlist ],
} );
Should you need a reference to an unsorted list as the return value, you
may call the unsorted option as follows:
$intersection_ref = get_intersection_ref(
'-u', [ \@Llist, \@Rlist ] );
$intersection_ref = get_intersection_ref(
'--unsorted', [ \@Llist, \@Rlist ] );
=head1 DISCUSSION
=head2 General Comments
List::Compare::Functional is a non-object-oriented implementation of very
common Perl code used to determine interesting relationships between two
or more lists at a time. List::Compare::Functional is based on the same
author's List::Compare module found in the same CPAN distribution.
List::Compare::Functional is closely modeled on the ''Accelerated''
mode in List::Compare.
For a discussion of the antecedents of this module, see the discussion of the
history and development of this module in the documentation to List::Compare.
=head2 List::Compare::Functional's Export Tag Groups
By default, List::Compare::Functional exports no functions. You may import
individual functions into your main package but may find it more convenient to
import via export tag groups. Four such groups are currently defined:
use List::Compare::Functional qw(:main)
use List::Compare::Functional qw(:mainrefs)
use List::Compare::Functional qw(:originals)
use List::Compare::Functional qw(:aliases)
=over 4
=item *
Tag group C<:main> includes what, in the author's opinion, are the six
List::Compare::Functional subroutines mostly likely to be used:
get_intersection()
get_union()
get_unique()
get_complement()
get_symmetric_difference()
is_LsubsetR()
=item *
Tag group C<:mainrefs> includes five of the six subroutines found in
C<:main> -- all except C<is_LsubsetR()> -- in the form in which they
return references to arrays rather than arrays proper:
get_intersection_ref()
get_union_ref()
get_unique_ref()
get_complement_ref()
get_symmetric_difference_ref()
=item *
Tag group C<:originals> includes all List::Compare::Functional subroutines
in their 'original' form, I<i.e.>, no aliases for those subroutines:
get_intersection
get_intersection_ref
get_union
get_union_ref
get_unique
get_unique_ref
get_unique_all
get_complement
get_complement_ref
get_complement_all
get_symmetric_difference
get_symmetric_difference_ref
get_shared
get_shared_ref
get_nonintersection
get_nonintersection_ref
is_LsubsetR
is_RsubsetL
is_LequivalentR
is_LdisjointR
is_member_which
is_member_which_ref
are_members_which
is_member_any
are_members_any
print_subset_chart
print_equivalence_chart
get_bag
get_bag_ref
=item *
Tag group C<:aliases> contains all List::Compare::Functional subroutines
which are aliases for subroutines found in tag group C<:originals>. These
are provided simply for less typing.
get_symdiff
get_symdiff_ref
is_LeqvlntR
=back
=head2 April 2004 Change of Interface
B<Note:> You can skip this section unless you used List::Compare::Functional
prior to the release of Version 0.25 in April 2004.
Version 0.25 initiated a significant change in the interface to
this module's various functions. In order to be able to accommodate
comparisons among more than two lists, it was necessary to change the type
of arguments passed to the various functions. Whereas previously a
typical List::Compare::Functional function would be called like this:
@intersection = get_intersection( \@Llist, \@Rlist ); # SUPERSEDED
... now the references to the lists being compared must now be placed
within a wrapper array (anonymous or named), a reference to which is
now passed to the function, like so:
@intersection = get_intersection( [ \@Llist, \@Rlist ] );
... or, alternatively:
@to_be_compared = (\@Llist, \@Rlist);
@intersection = get_intersection( \@to_be_compared );
In a similar manner, List::Compare::Functional functions could previously
take arguments in the form of references to 'seen-hashes' instead of
references to arrays:
@intersection = get_intersection( \%h0, \%h1 );
(See above for discussion of seen-hashes.) Now, those references to
seen-hashes must be placed within a wrapper array (anonymous or named),
a reference to which is passed to the function, like so:
@intersection = get_intersection( [ \%h0, \%h1 ] );
Also, in a similar manner, some List::Compare::Functional functions
previously took arguments in addition to the lists being compared.
These arguments were simply passed as scalars, like this:
@memb_arr = is_member_which(\@Llist, \@Rlist, 'abel');
Now these arguments must also be placed within a wrapper array
(anonymous or named), a reference to which is now passed to the function,
like so:
@memb_arr = is_member_which( [ \@Llist, \@Rlist ], [ 'abel' ] );
... or, alternatively:
@to_be_compared = (\@Llist, \@Rlist);
@opts = ( 'abel' );
@memb_arr = is_member_which( \@to_be_compared, \@opts );
As in previous versions, for a speed boost the user may provide the
C<'-u'> or C<'--unsorted'> option as the I<first> argument to some
List::Compare::Functional functions. Using this option, the
C<get_intersection()> function above would appear as:
@intersection = get_intersection( '-u', [ \@Llist, \@Rlist ] );
... or, alternatively:
@intersection = get_intersection( '--unsorted', [ \@Llist, \@Rlist ] );
The arguments to I<any> List::Compare::Functional function will therefore
consist possibly of the unsorted option, and then of either one or two
references to arrays, the first of which is a reference to an array of
arrays or an array of seen-hashes.
=head1 AUTHOR
James E. Keenan (jkeenan@cpan.org). When sending correspondence, please
include 'List::Compare::Functional' or 'List-Compare-Functional' in your
subject line.
Creation date: May 20, 2002. Last modification date: August 16 2020.
Copyright (c) 2002-20 James E. Keenan. United States. All rights reserved.
This is free software and may be distributed under the same terms as Perl
itself.
=cut
|