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...


In reply to Re^3: Method to Parse IP 2 Array by wjw
in thread Method to Parse IP 2 Array by djzah71

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.