Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:
I've got a problem with some code that I am sure I am going to end up kicking myself over, but...
I've built two hash tables - one from a config file ( hash a ) and the other from web data ( hash b ). The information in the tables is being used to display a series of check boxes. If my config file does in fact exist, I want to use that data to pre-check the boxes I'm displaying, so I'm testing for the existence of a key in hash a using the key from hash b.
My problem is that it doesn't think the key exists ever in hash b. I have verified through print statements and debugging that both hashes are populated, and with the same data.
Here's my code:
#!/usr/bin/perl -w use strict; use LWP::Simple; use HTTP::Request::Common; my @d; my ($stream, $file); my %list; my %cfg; $file = 'dave.cfg'; if ( -e $file ) { die "Failed: $! trying open existing $file\n" unless open FH, $fil +e ; &getinfo; &buildhash; #&printboxes; &test; } else { print "Didn't find file\n"; die "Failed: $! trying to create new $file\n" unless open FH, '> +'.$file; &buildhash; #&printboxes; } sub buildhash{ # This procedure gets the web page with the links to the comics and bu +ilds a # hash table consisting of the name of the comic as the key and the UR +L as the # value. # Get information from kingfeatures.com $stream = get('http://www.kingfeatures.com/features/comics/comicsN +av.htm'); @d = split('\n',$stream); my $end = $#d-1; # This information is taken from the side menu for my $count(0..$end){ if ($d[$count] =~ m!name="(.+)"><a href="(.+)" target="_top"!i +){ $list{$1} = $2; } } # Get information from comics.com $stream = get('http://www.comics.com/'); @d = split('\n',$stream); $end = $#d-1; # The information is taken from their drop down box menu for my $count(0..$end){ # This line can be uncommented if you want to include editorial comics + by artist # last if $d[$count] =~ m/editorial/i; if ($d[$count] =~ m!option\s+value="(.+)">(.+)</!i){ $list{$2} = $1; } } } # end buildhash sub getinfo{ my $line; $cfg{$line}=1 while ($line = <FH>); close FH; } sub test{ my $key; foreach $key (keys %list){ print "Value is $cfg{$key}\n" if exists $cfg{$key}; } }
Any help with this would be greatly appreciated.
There is no emoticon for what I'm feeling now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash question - using value of one hash to determine existence in second hash
by djantzen (Priest) on Nov 17, 2002 at 05:07 UTC | |
by Popcorn Dave (Abbot) on Nov 17, 2002 at 06:51 UTC | |
|
Re: Hash question - using value of one hash to determine existence in second hash
by Zaxo (Archbishop) on Nov 17, 2002 at 05:09 UTC |