Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comparing elements in 2 hashes...

by whisper80 (Initiate)
on Jul 02, 2002 at 18:20 UTC ( [id://178942]=perlquestion: print w/replies, xml ) Need Help??

whisper80 has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 files, each are inputed into a hash. Hash1 contains a list of things to be searched for Hash2 contains the file to be searched. I want to compare the elements in Hash1 with the elements in Hash2, and print the element if it matches. Any one know the best way to do this? Thanks

Replies are listed 'Best First'.
Re: comparing elements in 2 hashes...
by suaveant (Parson) on Jul 02, 2002 at 18:39 UTC
    assuming the lines are the keys...
    for(keys %Hash1) { print "$_\n" if exists $Hash2{$_}; }
    That'll do it... if you want case insensitive matching you can lowercase both files first...

    though you might be better off to read in the key file, and then go line by line through the main file looking up the lines in the key hash... save lots of memory, probably...

    then again... hashes may not be the way to go at all... what does your data look like?

    Update hrm... does look a bit like homework... but isn't it summer?

                    - Ant
                    - Some of my best work - (1 2 3)

      but isn't it summer?

      Australia? Or any of the other countries "down south"...
      --
      Mike

        You mean... they have SCHOOLS in AUSTRALIA?!?! But that blows my whole theory on the Crocodile Hunter! ;) (is he even really australian?)

                        - Ant
                        - Some of my best work - (1 2 3)

(wil) Re: comparing elements in 2 hashes...
by wil (Priest) on Jul 02, 2002 at 18:44 UTC
    You'll have to be a little bit more specific with your data structures, if you want a more specific answer. However, if you take a look through the Perl FAQ, you will stumble across the following FAQlet that offers various ways of doing what you're trying to do:

    How do I test whether two arrays or hashes are equal?

    - wil
Re: comparing elements in 2 hashes...
by whisper80 (Initiate) on Jul 02, 2002 at 19:18 UTC
    This is the code I have so far, for now I'm just trying to get it to print "YES" when it has found a match, but it's not working.
    #!/local/bin/perl -w $build = ''; print "Which build (enter full path name)? \n" ; chomp($build = <STDIN>); open(FILE, "/home/me/stuff/input.txt") || die "Could not open input file\n"; open(BUILD_FILE, $build) || die "Could not open input file\n"; %file_list = (); %build_file = (); while(<FILE>){ $file_list{$_} = 1; } while(<BUILD_FILE>){ $build_file{$_} = 1; } foreach $bkey (sort keys %build_file) { foreach $line(keys %file_list){ if ($line =~ $bkey){ print "YES\n"; } else{ print "NO\n"; } } } close(FILE); close(BUILD_FILE);
    no, this isn't homework, just something i'm supposed to put together for work. Thanks
      You're missing the point of hashes, I'm afraid...
      foreach $bkey (sort keys %build_file) { if (exists $file_list{$bkey}) print "YES\n"; } else{ print "NO\n"; } }
      The whole idea is that a hash lets you "instantly" look up whether a given key exists in it WITHOUT having to loop over all the keys...

      Your code would work, but it would take n*m time, where n's the number of entries in build file, and m's the number in file_list.

      This version takes about n time, m times quicker.
      --
      Mike

      Are you looking to get a bunch of "NO" responses? If so, you should just use arrays instead of hashes. As the code is currently written, you never use the "hash-iness" of %file_list and %build_file, and you might as well replace them both with arrays.

      If you are looking to only print out lines that are in both files, then you should read one file into a hash and the other into an array, then code something like this:

      foreach (@myArray) { print "$_\n" if ($myHash{$_}); }
      Hope this helps.

      -Ton
      -----
      Be bloody, bold, and resolute; laugh to scorn
      The power of man...

      I must say, I don't get it...why use hashes when you only use the keys?
      This one works, though I've taken the liberty of using arrays instead, adding use strict, declararations and so on. I tried it with two files, each containing three identical lines, and I got:
      Which build (enter full path name)? /tmp/database.txt NO NO YES YES NO NO NO YES NO
      -----------------
      #!/local/bin/perl -w use strict; my $build = ''; print "Which build (enter full path name)? \n" ; chomp($build = <STDIN>); my @build_file = (); my @file_list = (); open(FILE, "</home/me/stuff/input.txt") || die "Could not open input file\n"; while(my $Line = <FILE>){ push (@file_list, $Line); } close FILE; open(BUILD_FILE, '<' . $build) || die "Could not open input file\n"; while(my $Line = <BUILD_FILE>){ push (@build_file, $Line); } close BUILD_FILE; foreach my $bkey (sort @build_file) { foreach my $line(@file_list){ if (index($line, $bkey) >= 0){ print "YES\n"; } else{ print "NO\n"; } } }


      #!s #!s, oh baby when she moves, she moves...
perldoc perldata
by cebrown (Pilgrim) on Jul 02, 2002 at 18:38 UTC
    Is that homework I smell?

    In the meantime you can consider that by saying "compare the elements in Hash1 with the elements in Hash2, and print the element if it matches", you've phrased the question so well that's probably just a question of running "perldoc perldata", then reading and applying the results.

    If you'd like to post non-working code to show you've tried, you may have better luck.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://178942]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-24 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found