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)
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
(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 | [reply] |
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 | [reply] [d/l] |
|
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 | [reply] [d/l] |
|
foreach (@myArray) {
print "$_\n" if ($myHash{$_});
}
Hope this helps.
-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man... | [reply] [d/l] |
|
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... | [reply] [d/l] [select] |
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. | [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |