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

Find unmatched between an array and a hash

by hotyopa (Scribe)
on Feb 07, 2001 at 05:34 UTC ( [id://56872]=perlquestion: print w/replies, xml ) Need Help??

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

I have a hash:

my %languages = ( ASM => 'Assembler', PL => 'Perl', C => 'C', JV => 'Java', VB => 'Visual Basic', DLP => 'Delphi', );
and an array: my @employee = ('PL', 'VB', 'C'); What I want to do is come up with an array containing those unmatched keys from the hash. That is, in my example:

 my @result = ('ASM', 'JV', 'DLP');

I'm looking for a smart way to achieve this. I could do it with a loop and compare type thing, but am sure there is a clever and better way to do it.

(Background info: it's for a HRM skills database. I've read in the skill records for an employee, and want to put a text box at the top of the form from which they can add a new skill to their records).

*~-}hotyopa{-~*

Replies are listed 'Best First'.
Re: Find unmatched between an array and a hash
by japhy (Canon) on Feb 07, 2001 at 06:06 UTC
    I would do this:
    my %copy = %hash; delete @copy{@array}; my @left_over = keys %copy;
    If you don't mind being destructive to %hash, you can just delete from there.

    japhy -- Perl and Regex Hacker
Re: Find unmatched between an array and a hash
by Fastolfe (Vicar) on Feb 07, 2001 at 05:49 UTC
Re: Find unmatched between an array and a hash
by eg (Friar) on Feb 07, 2001 at 05:46 UTC

      This isn't slick, but it is straight-forward and seems like it should be fairly fast:

      my @result= do { my %temp; @temp{@employee}= (); grep ! exists $temp{$_}, keys %languages; }

      Then again, with Perl and an N of 6, I don't think O(N*N) is much of a problem. ;)

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://56872]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found