I started playing about with Perl for the first time yesterday as I've got some work coming up that it will be useful for. I'm trying to write a script that will take start and end IP's for a netblock and print out all the possible IP's inbetween but I'm having a little difficulty comparing the arrays that I'm using to store the current and end IP's. Here's my code so far:
#Get the range to be displayed from the user
print "Config start address \n";
$startAddress = <STDIN>;
print "Config end address \n";
$endAddress = <STDIN>;
#Get rid of the carrage returns
chomp($startAddress);
chomp($endAddress);
#Pack the start and end address into seperate arrays to be used.
#We also need an array to store the current address.
@start = split(/\./, $startAddress);
@end = split(/\./, $endAddress);
@currentAddress = @start;
#This is the loop that I am having problems with
while(@currentAddress != @end){
incAddress();
}
sub incAddress{
#We don't know how big the supplied netblock will be so provide functionality for every possibility.
printAddress();
if ($end[3] > $currentAddress[3]){
$currentAddress[3]++;
printAddress();
}
#Increase the value of the third octet by 1 and reset the fourth octet
if ($end[2] > $currentAddress[2] && $currentAddress[3] eq $end[3]){
$currentAddress[2]++;
$currentAddress[3] = 0;
printAddress();
}
#Increase the value of the seccond octet by 1 and reset the third and fourth octets
if ($end[1] > $currentAddress[1] && $currentAddress[2] eq $end[2]){
$currentAddress[1]++;
$currentAddress[2] = 0;
$currentAddress[3] = 0;
printAddress();
}
}
sub printAddress {
$printableAdd = join("\.", @currentAddress);
print $printableAdd;
}
I've found a few bits and pieces on the net about comparing specific variables with in a loop but I want to check that everything is the same. Do I need to manually iterate through each element of the arrays and compare them or is there a neater way of doing it?
Thanks for any help,
-N

New Topic/Question
Reply



MultiQuote






|