in reply to How to compare two associative arrays efficiently

hello, i read one column from two different flat files and stored the result into two associative arrays,

Associative arrays are for associating stuff, specifically keys with values. Now, independently of how you build them, we know that you got two such associative arrays. Supposedly the keys are entries from the first columns of each file, and other info missing one has to guess values are of no particular interest, and thus probably undef.

now I have to comapare these two arrays.

They're not arrays. They're associative arrays aka hashes. How do you want to compare them? Just find common keys?!?

If I use foreach loop, it will be very inefficient as it will loop thru n*n times.

You're saying more now, implying that both hashes have the same size. Indeed a hash must be though of as a mapping which also recalls the naive concept of a set which in turn recalls that of membership, thus it should come as no surprise that membership can be tested by means of straight features hiding the details from the programmer. Specifically chances are you just want to know about exists.

if they dont have common values then I have to store into another array. I am not sure how to do that. can anyone suggest something.

If they don't have common values (which values?!?) you have to store what into what? (really "another array" or another hash?)

  • Comment on Re: How to compare two associative arrays efficiently

Replies are listed 'Best First'.
Re^2: How to compare two associative arrays efficiently
by viveks_19 (Initiate) on Oct 12, 2006 at 21:25 UTC
    thanks, but again have a doubt. I got that my arrays(hashes) have only keys and values are undef. but my flat file is like 1111 2222 3333 4444 which I have loaded into an an hash say %hash1 and I was assuming that with this 1111 and 3333 will become the key for values 2222 and 4444 respectively, but I think I was wrong. So I have two flat files just like the above sample and which I have loaded into two separate hashes say %hash1,%hash2, and if they have any not common values/key then store into another array or hash, does not matter. Please help me out...

      It can be loaded either way. It's totally up to you how you store the info in the hash.

      Anyway, here's code that should accomplish your goal.

      use strict; use warnings; my $fn_assn = ...; my $fn_pssn = ...; my @assns; { open(my $fh_assn, '<', $fn_assn) or die("Unable to open assn file \"$fn_assn\": $!\n"); chomp(@assns = <$fh_assn>); } my @pssns; { open(my $fh_pssn, '<', $fn_pssn) or die("Unable to open pssn file \"$fn_pssn\": $!\n"); chomp(@pssns = <$fh_pssn>); } { my %pssns = map { $_ => 1 } @pssns; my @unique_assns = grep { not exists $pssns{$_} } @assns; print("The following ASSNs have no corresponding PSSNs:\n"); print("$_\n") foreach @unique_assns; } print("\n"); { my %assns = map { $_ => 1 } @assns; my @unique_pssns = grep { not exists $assns{$_} } @pssns; print("The following PSSNs have no corresponding ASSNs:\n"); print("$_\n") foreach @unique_pssns; }
      thanks, but again have a doubt. I got that my arrays(hashes) have only keys and values are undef. but my flat file is like 1111 2222 3333 4444 which I have loaded into an an hash say %hash1 and I was assuming that with this 1111 and 3333 will become the key for values 2222 and 4444 respectively, but I think I was wrong.

      Indeed if you do something like

      my %hash=qw/1111 2222 3333 4444/;

      then %hash will have the structure you describe. However generally keys and associated values have a "different nature". Nothing prohibits them to be homogeneus, but I see no reason for them to be so in this case, so is it really what you want?

      So I have two flat files just like the above sample and which I have loaded into two separate hashes say %hash1,%hash2, and if they have any not common values/key then store into another array or hash, does not matter. Please help me out...

      I wish I could help you but it's hard to make sense of "common values/keys". Also despite your efforts the expression "then store into another array or hash" is still missing the object to be stored into whatever.