The previous two posts explain why you're getting the error. If you want to keep the 'site' key in the hash for whatever reason (even though it seems redundant), you can simply add another layer so it follows the HoH structure of the other data. That way all of the keys in %hash are hashrefs:

$hash{$ebiz_siebel_dns}{site} = $ebiz_siebel_dns; $hash{$ebiz_siebel_dns}{ip_now} = $ebiz_siebel_ip; $hash{$ebiz_siebel_dns}{ip_new} = $ebiz_siebel; for my $j ( keys(%hash) ){ print "site -> $j\n"; # $j eq $hash{$j}{site} print "$j -> $hash{$j}{ip_now}\n"; print "$j -> $hash{$j}{ip_new}\n"; }

It also seems that you are creating a lot of unnecessary variables (three for each site). You might want to consider restructuring this block something like this:

my %sites = ( 'messenger.hotmail.com' => { ipnow => '207.46.104.20', type => 'A' }, 'www.siebel.com' => { ipnow => '64.181.189.55', type => 'A' }, ); foreach my $dns ( keys %sites ) { $sites{$dns}{ipnew} = nslookup( host => $dns, type => $sites{$dns}{type} ); } foreach my $dns ( keys %sites ) { print "$dns\n"; print "$dns -> $sites{$dns}{ipnow}\n"; print "$dns -> $sites{$dns}{ipnew}\n"; }

This way, if you want to add (or remove) a site from the list, you simply edit the %sites hash and leave the rest of the code alone. It also keeps all of the nslookup calls together, and (IMO) it makes it easier to follow the code.

HTH


In reply to Re: Problem with Hash when Using Strict by bobf
in thread Problem with Hash when Using Strict by Dru

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.