Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Check if keys of hash is values of another hash!

by stmadalli (Initiate)
on Jul 15, 2014 at 18:08 UTC ( #1093754=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have the following code where I need to a IF Condition to check if a keys of %line is one of the values of %new.. please let me know if this is possible!
#!/usr/bin/perl use strict; use warnings; my @array=('a','b','c'); my %list=( 'a' => ["100","nuts"], 'b' => ["200","bolts"], 'c' => ["300","screws"], ); my $msg = getMsg(\@array, \list); #print $msg; sub getMsg { my $aref = shift; my $href = shift; my %line = ('100' => ["abc","xyz"] 'nuts' => ["def","lmn"] ); my @arr = @$aref; my %new = %$href; if(exists {Keys of %line contains any values of $new{a} }) #need this + condition { do something else { do something } }

Replies are listed 'Best First'.
Re: Check if keys of hash is values of another hash!
by kennethk (Abbot) on Jul 15, 2014 at 18:46 UTC

    First, please make sure posted code compiles. Ignoring your pseudocode, you are missing a sigil on line 16 and a comma on line 22 and a curly bracket on line 31. See How do I post a question effectively?.

    If a defined condition is sufficient (rather than exists), then you can easily perform the check with a grep and Slices:

    if(grep defined, @line{@{$new{a}}}) #need this condition
    Otherwise, it's probably easiest to loop over the array ref:
    my $seen = 0; for my $val ( @{$new{a}} ) { $seen++ if exists $line{$val}; } if($seen) {#need this condition do something } else { do something }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Check if keys of hash is values of another hash!
by kcott (Bishop) on Jul 15, 2014 at 19:13 UTC

    G'day stmadalli,

    Welcome to the monastery.

    if(exists {Keys of %line contains any values of $new{a} }) #need this +condition

    I suspect what you want is something like this:

    if (grep { exists $line{$_} } @{$new{a}}) {

    There are a number of syntactical problems with the code you posted. Compare it with this example:

    #!/usr/bin/env perl -l use strict; use warnings; my @array = qw{a b c}; my %list = ( a => [qw{100 nuts}], b => [qw{200 bolts}], c => [qw{300 screws}], ); my $msg = getMsg(\@array, \%list); sub getMsg { my ($aref, $href) = @_; my %line = ( 100 => [qw{abc xyz}], nuts => [qw{def lmn}], ); for my $key (@$aref) { print 'Key: ', $key; if (grep { exists $line{$_} } @{$href->{$key}}) { print 'TRUE'; } else { print 'FALSE'; } } }

    [Note how I've used qw{...} to eliminate much of the original punctuation so that the data is not lost amongst a forest of quotation marks (see perlop: Quote and Quote-like Operators). Also note the fat comma, =>, quotes its left operand so that's several more quotes that aren't needed (see perlop: Comma Operator).]

    Output:

    Key: a TRUE Key: b FALSE Key: c FALSE

    I don't think these are what you meant, but be aware of the built-in functions keys and values.

    Also look at the first function provided by the built-in module List::Util. This can save you checking all elements of an array (with grep) when you only want to know if any element meets your condition (i.e. it stops checking as soon as the first TRUE condition is found).

    -- Ken

      Thanks for the quick reply! I will correct my programs before I post :) This worked for me: if(grep defined, @line{@{$new{a}}}) Thanks!
Re: Check if keys of hash is values of another hash!
by Bloodnok (Vicar) on Jul 15, 2014 at 18:38 UTC
    Methinx that something along the lines of if (grep { exists $b{$_} } keys %a) { ... might be one of the simpler solutions.

    HTH,

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2020-08-10 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?