#!/usr/bin/perl use strict; use warnings; my $file = "input.txt"; open(IN, "<", $file) or die (Can't open $file, $!\n"); my @in = ; chomp @in; #all records(lines) from the file are now in array @in and line endings have been removed(\n or \r\n) close IN; #so you don't need to hold the file open anymore, close it 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... }