Suwen has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I want to extract the last portion of an IP address. For example,
I want to get uk from www.xxx.xxx.uk, and assign United Kingdom to another variable.
My code is as below, but it dosen't work. I would like to get some advice from you.
Thank you for your attentionmy $remote_host = gethostbyaddr(inet_aton ($ip_addr),AF_INET); # get www.xxx.xxx.uk my %CountryCode = { 'com','Comercial', 'uk','The United Kingdom', } # a hash of country name my $CountryName; my @CCode = split /./, $remote_host; # split every portion my $Country = $CCode[-1]; # get the last foreach $key (keys %CountryCode) { if ($Country == $key){ #get the key of country name $CountryName = $CountryCode($key); #assign the key value to a +variable } }
Edit Masem 2001-07-26 - CODE tags added, BR tags removed
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to do with this hash?
by suaveant (Parson) on Jul 27, 2001 at 00:13 UTC | |
|
Re: How to do with this hash?
by da (Friar) on Jul 27, 2001 at 00:16 UTC | |
|
Re: How to do with this hash?
by tadman (Prior) on Jul 27, 2001 at 06:03 UTC | |
|
Re: How to do with this hash?
by jlongino (Parson) on Jul 27, 2001 at 08:58 UTC |