in reply to Method to Parse IP 2 Array

Perhaps something like this?

#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { chomp $line; if ($line =~ m/in-addr/) { my @line = split(/\./, $line); if ($line[2] =~m/in-addr/) { process_line("$line[1]\." ."$line[0]\." . "0\." . "0" . " +\/24"); } else { process_line("$line[2]\." . "$line[1]\." ."$line[0]\." . " +0" . "\/16" ); } } else { process_line($line); } } sub process_line { print shift . "\n"; # do whatever... } __DATA__ 250.26.192.in-addr.arpa,3 251.26.192.in-addr.arpa,3 252.26.192.in-addr.arpa,3 253.26.192.in-addr.arpa,3 254.26.192.in-addr.arpa,3 26.192.in-addr.arpa,2 hosangit.com djzah.com

Output

192.26.250.0/16 192.26.251.0/16 192.26.252.0/16 192.26.253.0/16 192.26.254.0/16 192.26.0.0/24 hosangit.com djzah.com

Hope that is helpful....

...the majority is always wrong, and always the last to know about it...

Insanity: Doing the same thing over and over again and expecting different results...

Replies are listed 'Best First'.
Re^2: Method to Parse IP 2 Array
by djzah71 (Initiate) on May 22, 2014 at 23:58 UTC
    Thank You, that makes more sense. Could you give me direction on how to use an input.txt file instead of putting everything under the __DATA__ section

      Anonymous is correct, you could use those modules, in the interest of sticking with very basic code...

      #!/usr/bin/perl use strict; use warnings; my $file = "input.txt"; open(IN, "<", $file) or die (Can't open $file, $!\n"); my @in = <IN>; chomp @in; #all records(lines) from the file are now in array @in an +d line endings have been removed(\n or \r\n) close IN; #so you don't need to hold the file open anymore, close i +t while (my $line = @in){ #you could do a foreach here too, the + rest of this should be familiar to you if ($line =~ m/in-addr/) { my @line = split(/\./, $line); if ($line[2] =~m/in-addr/) { process_line("$line[1]\." ."$line[0]\." . "0\." . "0" . " +\/24"); } else { process_line("$line[2]\." . "$line[1]\." ."$line[0]\." . " +0" . "\/16" ); } } else { process_line($line); } } sub process_line { print shift . "\n"; # do whatever... }

      It is assumed that your text file is not gigabits in size, thus read it into an array instead of holding the file open. You could also just hold the file open and read it line by line, then close it when processing is complete. My preference is to not do that. And it is nothing but a preference...

      I did not test this time, so you may find an typo or two, but am sure you can handle that with warnings turned on and all...

      If you are just learning Perl, you might want to fire this up using 'perl -d (whatever you name this script). The debugger (-d) is a nice way to step through and find out how things work. You can find info in the debugger here, in the pods, or on the web...

      enjoy

      ...the majority is always wrong, and always the last to know about it...

      Insanity: Doing the same thing over and over again and expecting different results...