Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Using hashes for set operations...

by ikegami (Pope)
on May 21, 2011 at 17:00 UTC ( #906073=note: print w/ replies, xml ) Need Help??


in reply to Using hashes for set operations...

my %set1 = map { $_ => 1 } @array1; my %set2 = map { $_ => 1 } @array2; my %intersection = grep $set1{$_}, keys %set2;

That assumes true values. You're using weird values, so you might have to use exists.


Comment on Re: Using hashes for set operations...
Select or Download Code
Replies are listed 'Best First'.
Re^2: Using hashes for set operations...
by LanX (Canon) on May 21, 2011 at 18:50 UTC
    well this my %intersection = grep $set1{$_}, keys %set2; is wrong, you're assigning a list of keys to a hash.

    and you missed so many points from my post that I assume that you only read the headings... or how do you want to intersect sets of objects like this?

    Cheers Rolf

      and [he] missed so many points from my post that I assume that you only read the headings
      I've noticed a trend for quantity at the expense of quality, in general.
        Answering the replies to my posts has been taking a lot out of me, and taking a lot of time away from my other answers.

      Sorry, missing map { $_ => 1 }

      my %set1 = map { $_ => 1 } @array1; my %set2 = map { $_ => 1 } @array2; my %intersection = map { $_ => 1 } grep $set1{$_}, keys %set2;

      and you missed so many points from my post that I assume that you only read the headings..

      Are you trying to imply that I should have told you you need to use a hash to get the original from the stringification even you already know that?

      Or are you referring to request to avoid loops? Yeah, I missed that bit of nonsense. It's impossible to find the intersection of two sets without looping over the two sets.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2015-07-28 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (257 votes), past polls