Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found