in reply to comparing elements in 2 hashes...

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

Replies are listed 'Best First'.
Re: Re: comparing elements in 2 hashes...
by RMGir (Prior) on Jul 02, 2002 at 19:49 UTC
    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

Re: Re: comparing elements in 2 hashes...
by ton (Friar) on Jul 02, 2002 at 19:56 UTC
    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...

Re: Re: comparing elements in 2 hashes...
by PerpLexicon (Novice) on Jul 02, 2002 at 19:57 UTC
    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...