Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:54:07+00:00 2026-06-08T20:54:07+00:00

I am pretty new to Perl and need to accomplish a task quickly. Any

  • 0

I am pretty new to Perl and need to accomplish a task quickly. Any help is appreciated!

I have two hash of arrays as follows:

Hash 1
-------
w: ['A','B','C']
e: ['P','Q','R']

Hash 2
-------
w:['A','B','C']
e:['P','Q','O']
r:['S','T']

Context:

  1. I want to find the differece in values for same keys(e.g Hash 1 does not have
    value ‘O’ from Hash 2 for the same key ‘e’.

  2. Find the difference in keys. (e.g ‘r’ is not present in hash 1.

I put together some code but it checks the exact value of the complete line from two hashes. For example If I have ‘A’,’B’,’C’ in hash 1 for key w and ‘B’, ‘C’,’A’ in hash 2 for same key if flags a difference. I want to compare value by value-

Following code compares two hash os arrays. So from the above example A,B,C from hash 1 is not equal to B , A,C from hash 2. However I want to check the existence of the individual item say A in and not worry about the order.

for ( keys %hash2 ) 
{     
    unless ( exists $hash1{$_} ) # Checks for mismatches in keys
    {         
        print "$_: Key mismatch $_ received \n"; 
        next;     
    }      

    if ( $hash2{$_} eq $hash1{$_} ) #Compares two lines exactly         
    {        
        print "$_: No mismatch \n";  
    }     
    else 
    {       
        print "$_: Value mismatch for key $_ \n";  #Difference in Value
    } 
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:54:09+00:00Added an answer on June 8, 2026 at 8:54 pm

    If you don’t care about orders, just compare the ordered set of values:

    your code:

    if ( $hash2{$_} eq $hash1{$_} ) #Compares two lines exactly      
    

    Should be:

    if (     join(",", sort @{ $hash1{$_}})
          eq join(",", sort @{ $hash2{$_}}) ) #Compares two lines exactly      
    

    On the other hand, if you want to compare memberships of arrays, simply turn array into a hashref:

    foreach my $key2 ( keys %hash2 ) {
        unless ( exists $hash1{$key2} ) { print ""; next; };
    
        my %subhash1 = map { ( $_ => 1 ) } @{ $hash1{$key} };
        my %subhash2 = map { ( $_ => 1 ) } @{ $hash2{$key} };
    
        # Compare 2 subhashes same way you are comparing parent hashes in inner loop
    
        foreach my $subkey2 ( keys %subhash2 ) {
            # Check for exists
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still pretty new to perl and regex and need some help getting started.
I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
I'm pretty new to Perl but have been programming in java for several months
Pretty new to Perl so there may be a very obvious solution here. I'm
Pretty new to MVC and the like. I have a class the looks like
I pretty new to Objective-C (and C itself) and need to consume a NSData
I'm pretty new to NLP in general, but getting really good at Perl, and
I'm stuck on this and have been all day.. I'm still pretty new to
I'm pretty experienced with Perl and Ruby but new to Python so I'm hoping
Pretty new to the Magento e-commerce solution. Have it up and running for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.