Gossamer Forum
Home : General : Perl Programming :

Stripping subnets from an array

Quote Reply
Stripping subnets from an array
Howdy fellow perl friends,

I have an array of IP addresses as $hst

I need to take each of the items in $hst, strip the last few digits from them (such as 161.184.220.89 to 161.184.220) so we average correctly.

Yes, I am one of those cut & pasters of all codes, and master of none! Can anyone help me out on this? I'm seeing some simple regexp, but it is eluding me....

Thanks!

Diversity by Design
Quote Reply
Re: [diversity] Stripping subnets from an array In reply to
I think I figured it out!

$hst =~ s/(\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+))$//;

Geez.. these regular expressions are hard. Let's do some graphics instead!

Diversity by Design
Quote Reply
Re: [diversity] Stripping subnets from an array In reply to
Tricky things these expressions. It wasn't consistant on every IP address!

This one appears to be working much better.
$hst =~ s/(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))$//;

Found this great snippett site, helped a bunch
http://www.regexlib.com