http://qs321.pair.com?node_id=817827

remzak has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to perl, yet an old dog with other languages. I'm looking for a way to scan through a very long list of 64-bit integers and find all pairs in the list that share only the least significant bit being set (($num1 & $num2)==1). The code below takes much too long.
# find all integers in the list that do not share any bits except the +lsb. my %MatchedIntegers=(); #my output foreach my $i1 (@LongListOfIntegers) { foreach (@LongListOfIntegers) { #if a match (only least significant bit overlaps), # add to hash of hash, I don't care about the value. $MatchedIntegers{$i1}{$_}=() if (($i1 & $_) == 1) ; } }
Any ideas? It seems that this should be a very fast thing to do. For a list of ~4,000 values, it takes 2 seconds. I want to keep the code in perl (not link to C/C++). I need the output in a hash table, but it could be a hash of arrays instead of a hash of hashes. Thanks,