IP address, in perl – the simple not so effective way.

What pattern could be used to match an IP address such as 192.168.53.124, where each part of
the address is a number from 0 to 255?

I first tried working on this using regex and then abandoning it. The problem was not the “format” of the ip address persay, but more of the limitation of it having to be less that 255.  Not too thrilled with it, and exploring the regex way, which I shall update (if I dont loose interest) sometime .. let me know if you have any thoughts on the regex version of it.


#! /usr/bin/perl -w
use strict;

print " Hey you, enter a sentence or a word\n";

my $line=<STDIN>;

my @splitline;

chomp $line;

print "line is $line";

@splitline=split(/\./,$line);

if(scalar(@splitline)==4){
 if($splitline[1]<=255){
 if($splitline[2]<=255){
 if($splitline[3]<=255){
 if($splitline[4]<=255){
 print ("Yayy!, you have a valid address\n");
 }
 else{
 print ("Not a valid address beep beep \n");
 }
 }
 else{
 print("Not a valid address beep beep \n");
 }
 }
 else{
 print("Not a valid address beep beep \n");
 }
 }
 else{
 print("Not a valid address beep beep \n");
 }
}
else{
 print ("yikees you don't even have the correct format");

couple of take aways:

1. Formating of the if’s

2. Length of the array can be found by forcing it as a scalar, also by saying $t=$#array

3. the length inbuilt function only acts on scalars.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s