Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: To Hash or Not to Hash??

by l2kashe (Deacon)
on Dec 02, 2003 at 15:38 UTC ( [id://311646]=note: print w/replies, xml ) Need Help??


in reply to To Hash or Not to Hash??

There is an issue here. In your test structure you are testing for $our{'ns1.t'}, but ns1.t is a value not a key. What you would need to test for would be a key.. ala

my $ip = '24.56.102.10'; my %ours = ( 24.56.102.10 => 'ns1.t', ); if ( $ours{$ip} ) { print "Its there\n"; } else { print "Its not there\n"; }

If I am reading deep enough into what you are trying to do, I think you want to also reverse your %our and %theirs hashes. ala

$ours{$v} = $k while ( my($k, $v) = each %ours);

This way you can test for the existance of either the name of the host, or the IP address of the host.

On a side note in regards to style, it is better to pass the data you are going to be working on to your sub.

#... yada yada ... my %ips = ( ours => { 24.56.102.10 => 'ns1.t', }, theirs => { 68.168.192.17 => 'ns1.a', }, ); my @recs = nslookup( type => "NS", domain => $domain, ); ours(\%ips, \@recs); # .. yada yada .. sub ours { my($ip, $recs) = @_; for ( @$recs ) { if ( $ip->{ours}{$_} ) { print OUT "$_\n"; } elsif ( $ip->{theirs}{$_} ) { print OUT1 "$_\n"; } else { # do we care about this case? } } }

This way its clear exactly what is going where, and what data is being worked on. As opposed to digging into the sub, figuring out what variables are used, determine their scope, etc..

use perl;

Replies are listed 'Best First'.
Re: Re: To Hash or Not to Hash??
by gwadej (Chaplain) on Dec 02, 2003 at 16:48 UTC

    As a quick side note, an idiom for inverting a hash like that is

    %ours = reverse %ours;

    This may not be the right choice if the hash is very large because of the extra memory used by the temporary list. But in this case, it looks like it would be okay.

    See recipe 5.8 in the Perl Cookbook.

    Obviously, both of our solutions will have problems if the values are not unique.

    G. Wade

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://311646]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-23 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found