in reply to Re^2: Method to Parse IP 2 Array
in thread Method to Parse IP 2 Array
Anonymous is correct, you could use those modules, in the interest of sticking with very basic code...
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...#!/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... }
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
Insanity: Doing the same thing over and over again and expecting different results...
|
|---|