Fellow monks,

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.


In reply to Hash question - using value of one hash to determine existence in second hash by Popcorn Dave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.