Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Is it possible to split an element of an array in Perl?

Posted on 2009-04-17
3
Medium Priority
?
393 Views
Last Modified: 2012-06-27
I am VERY new to perl so forgive me if I use some weird terminology.

OK.. So I have a text file with about 230,000 subnets and CIDR notations in it.  The subnet and CIDR notations are separated by a space instead of the usual /.

example of text file:

74.54.94.0 24
75.54.25.0 23

So I am going to have to dig all of these subnets to see what has a PTR record and which ones are missing.  I am happy with digging one IP address from each subnet.  Knowing how our tools work here if the .10 address has an existing PTR record I would be 99% sure that all of the subnet has its PTR records.  The problem comes for me is that if it is  a /23 I would have to increment the third octect of the subnet by one and dig it also.  

Ok so here is what I have so far:

[code]
#!/usr/bin/perl
while(<>)
{
#remove newline
chomp;

#seperate line on space
@f = split / /;

if (@f[1] == 24){
$line1=`dig +short -x @f[0]`;
print "$line1";
}
if (@f[1] == 23){
#seperate the octets and make new array called tt (twenty three)
$subnet=@[f0];
print "$subnet";
        }
}
[/code]

As you can see (probably not from my dumb coding) I would like to split the 0 element of the @f array with the delimited of . meaning each octet would now be another element of a new array.  I hope I am explaining my problem right.  Either way I have tried a few different ways.  The last attempt shown in my coding is me trying to make a new variable from an element in the first array to split again.  

Any help would be appreciated.
0
Comment
Question by:savone
  • 2
3 Comments
 
LVL 23

Author Comment

by:savone
ID: 24172671
One more try, this didnt work either! :(

#!/usr/bin/perl
while(<>)
{
#remove newline
chomp;
 
#seperate line on space
@f = split / /;
 
if (@f[1] == 24){
$line1=`dig +short -x @f[0]`;
print "$line1\n";
}
if (@f[1] == 23){
#seperate the octects and make new array called tt (twenty three)
$subnet=@f[0];
@subnet1 = split(/./, $subnet);
print "@subnet1[0]";
print "$subnet\n";
        }
}

Open in new window

0
 
LVL 25

Accepted Solution

by:
lwadwell earned 1000 total points
ID: 24172729
the period mark in a perl regexp matches to ANY value ... to match a period exactly you need to 'escape' it with a slash \.

ie.
@subnet1 = split(/\./, $subnet);
0
 
LVL 23

Author Comment

by:savone
ID: 24172818
AHH... I knew I was doing one little thing wrong... Ok... now I have this and it seems to be working!!


#!/usr/bin/perl
while(<>)
{
#remove newline
chomp;
 
#seperate line on space
@f = split / /;
 
if (@f[1] == 24){
$line1=`dig +short -x @f[0]`;
print "$line1\n";
}
if (@f[1] == 23){
#seperate the octects
$subnet = @f[0];
@s1 = split(/\./, $subnet);
#Increment by two
@s1[2]++;
$newsubnet = `echo "@s1[0]"."@s1[1]"."@s1[2]"."10"`;
$line2=`dig +short -x $newsubnet`;
print "$line2\n";
 
        }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question