Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Printing first and last line

by maheshkumar (Sexton)
on Aug 15, 2012 at 10:19 UTC ( [id://987529]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Printing first and last line
in thread Printing first and last line

Just one last quick question what i am trying to do is to compare the two IPs if the first three octet are similar or not. For example Destination to 92.123.72.112 but reached 92.123.72.0 in this the first three are similar that means 92.123.72 are same so i want to put these two IPs in a hash and the key to them will be their location which i will find with another code. How is this possible?

Replies are listed 'Best First'.
Re^5: Printing first and last line
by Athanasius (Archbishop) on Aug 15, 2012 at 11:16 UTC

    I think the following little script should be enough to show you how to proceed:

    #! perl use strict; use warnings; use diagnostics; use Data::Dumper; my $destination = '92.123.72.112'; my $final_ip = '92.123.72.0'; # Get the first 3 octets of each IP my $destn_prefix = $destination =~ s/ \. \d{1,3} $ //rx; my $final_prefix = $final_ip =~ s/ \. \d{1,3} $ //rx; # Populate the hash my %hash; if ($destn_prefix eq $final_prefix) { $hash{$destn_prefix} = [ $destination, $final_ip ]; } print Dumper(\%hash), "\n"; # Access a given location my $ip = '92.123.72'; if (exists $hash{$ip}) { my @ips = @{ $hash{$ip} }; print 'IPs at location ', $ip, ' are: ', join(', ', @ips), "\n"; }

    Output:

    $VAR1 = { '92.123.72' => [ '92.123.72.112', '92.123.72.0' ] }; IPs at location 92.123.72 are: 92.123.72.112, 92.123.72.0

    Some useful references:

    Hope that helps,

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found