#!/usr/bin/perl use strict; use warnings; use 5.10.0; # Build a few lists my @lists; for my $i (0 .. 1+int(3*rand)) { # several lists to compare push @{$lists[$i]}, int 16 * rand for 1 .. 15; print "LIST $i: ", join(", ", @{$lists[$i]}), "\n"; } # Track the items found in each list, including the order in # which they appear my %tally; for my $listnum (0 .. $#lists) { # Get a list my $list=$lists{$listnum}; for my $itemnum (0 .. $#$list) { # Get next item from the list my $item = $$list[$itemnum]; # Each item slot (value in list) gets an array, # where each slot is associated with a list. # In that array is an array of indexes into the # list where each item is found push @{$tally{$item}[$listnum]}, $itemnum; } } # Show which items were in all lists my $all = (1<<$bit)-1; print "There were $bit lists. Items found:\n"; for my $item (sort {$a<=>$b} keys %tally) { print "Item $item: "; if ($tally{$item} == $all) { print "in *all* lists\n"; next; } my @present; for ($bit=0; $bit<@lists; $bit++) { push @present, $bit if $tally{$item} & 1<<$bit; } print "in lists: ", join(", ", @present), "\n"; }