Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Break up a variable

Status
Not open for further replies.

qwicker

Programmer
Feb 16, 2005
12
0
0
US
hello, I am quite new to perl and i am not sure how do do something. I have a string $ip = "153.104.232.232" for example, and i need to compare the first 8 characters of to another IP address. Basically i want to check if that IP is part of the correct subnet. So i need the function that will "cut up" $ip and only return "153.104." so that i can compare it. How can i do this? Thank you

Example:
$ip = "153.104.233.234";
cut-up(8) $ip;
if ($ip=="153.104."){
blah blah
 
There are a few ways to do this. My favourite would be this.

$ip = "153.104.233.234";

$ip_copy = $ip;
$ip_copy =~ /\./; # look for a '.'
$first = $`; # everything before the '.'
$ip_copy = $'; # save everything *after* that '.'
$ip_copy =~ /\./; # look for another '.'
$second = $`; # everything before the '.'

print "first = $first and second = $second\n";


Mike

I am not scrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Using a regex
Code:
my $ip = "153.104.233.234";
my ($octets) = $ip =~ /^(\d+\.\d+)/; # apply match operator to $ip
# anything captured ("153.104") is stored in $octets
 
Code:
$octet1="103";
$octet2="94";
my (@octets)=split (/\./, $ip);
if (($octet[0]=$octet1) && ($octet[1] eq $octet2))
#  we have a match
}
TIMTOWTDI ;-)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
[b]#!/usr/bin/perl[/b]

$match = '[b][red]153.104[/red][/b]';

@addresses = qw(  [b][blue]143.104.233.234  153.102.233.234  153.104.233.234  153.100.233.234  153.104.233.234[/blue][/b]  );

foreach $ip (@addresses) {
  @octets = split(/\./, $ip);
  if ( "$octets[0].$octets[1]" eq $match ) {
    print "[b][green]*** MATCH ***[/green][/b]\n";
  } else {
    print "NO MATCH!\n";
  }
}

Kind Regards
Duncan
 
Dunc, do you need to escape that period in double quotes?

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi Paul

Doesn't appear to be necessary - i have tested the script & it seems to work fine

Kind Regards
Duncan
 
What about:

my ($oct1, $oct2, $oct3, $oct4 ) = split (/\./, $ip);

Mike

I am not scrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $IP1 = "192.168.3.123";
my $IP2 = "192.167.3.123";
my $IP3 = "195.164.3.12";

sub IPsort{
   pack('C4' => $a =~
      /(\d+)\.(\d+)\./)
   cmp
   pack('C4' => $b =~
      /(\d+)\.(\d+)\./)
}

my @arr = sort IPsort ($IP1, $IP2, $IP3);

foreach (0 .. $#arr) {
   print "$arr[$_]\n";
}

Corwin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top