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


In reply to Re: Check if keys of hash is values of another hash! by kcott
in thread Check if keys of hash is values of another hash! by stmadalli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.