You have an answer to your immediate problem, but there is something else that you may wish to consider.

Depending on the size of your log file, and the specs of your machine, reading the entire log file into an array may not be such a good idea. This is because memory must be allocated for the entire file. And if you happen to be dealing with a multi-gigabyte file, this may become a problem.

Generally, a better approach is to read the file line by line, and process each line as you go. For this, a while loop can be used. Example:

#!/usr/bin/perl use strict; use warnings; # 3 argument form of open using a lexical variable is considered bette +r practice # see perldoc -f open open my $file, '<', $ARGV[0] or die "Can't open $ARGV[0] for reading: +$!\n"; while (my $line = <$file>) { print $line; chomp($line); #if you need to # any other processing.... }
Using the above method, only one line of the file is read into memory at a time, which is much more memory-efficient.

Hope this helps,
Darren :)


In reply to Re: Split on new line by McDarren
in thread Split on new line by Karger78

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.