Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: intersection of N arrays

by jarich (Curate)
on Jul 07, 2004 at 15:14 UTC ( #372409=note: print w/ replies, xml ) Need Help??


in reply to Re^2: intersection of N arrays
in thread intersection of N arrays

Wouldn't this fail if the same element appears mulitple times in an array, such that the number of times it appears overall is == num of arrays? This code assumes all elements are unique in each array.

The provided code does not make that assumption. The key lines are:

foreach my $k (keys %ids) { my %uniq = map { $_ => 1 } @{ $ids{$k} }; $count[$_]++ for keys %uniq; }
If the array in $ids{$k} has a whole bunch of 1s in it, that's okay, we'll overwrite $uniq{1} a whole bunch of times. Because hash keys must be unique.

This means that at the last statement in this loop %uniq will represent a hash with only the unique values from $ids{$k}

I hope this helps.

jarich


Comment on Re^3: intersection of N arrays
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://372409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-08-01 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (52 votes), past polls