Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

In your question you should post an example of the data structure you are working with. Your description says that you are dealing with a hash of arrays, and that for every key there are two values. In some cases those two values are the same, and in other cases they are not. Since you're using substr in your example, I'll assume that "the same" means stringwise equality.

I'm imagining that your datastructure looks like this:

my %hash = ( foo => ['this', 'that'], # Two different values bar => ['those', 'those'], # Two identical values baz => ['the', 'other'], # Not the same buzz => ['same', 'same'], # The same );

In this structure you would want to print the name of the bar key and buzz key, since the values in the array ref for those keys are the same. You didn't suggest that there could ever be more or less than two values, so I won't solve the problem that isn't stated. Here's an example of how to accomplish what you're after given this data structure:

my %hash = ( foo => ['this', 'that'], # Two different values bar => ['those', 'those'], # Two identical values baz => ['the', 'other'], # Not the same buzz => ['same', 'same'], # The same ); while (my ($key, $aref) = each %hash) { if ($aref->[0] eq $aref->[1]) { print "$key\n"; } }

The output now should be 'bar' and 'buzz'.

If there could be fewer than two elements to compare, you could do this:

if (2==@$aref && $aref->[0] eq $aref->[1]) ...

If there could be more than two, and you want to assure that they are all equal, you could do this:

while (my ($key, $aref) = each %hash) { my %seen; $seen{$_}++ for @$aref; next if 1 != keys %seen; print "$key\n"; }

If these scenarios don't match the problem you are trying to solve, please refine your question to remove the ambiguity that has resulted in a diverse selection of answers.


Dave


In reply to Re: extracting only those keys with uniq values in array list by davido
in thread extracting only those keys with uniq values in array list by v15

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 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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-06-14 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (58 votes). Check out past polls.

    Notices?