Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I split a file into an array using blank lines as delimiter

( #47236=categorized question: print w/ replies, xml ) Need Help??
Contributed by aaAzhyd on Dec 18, 2000 at 22:33 UTC
Q&A  > files


Description:

I have a file such as:

--
blah blah blah
blah blahblahblalh

oh yeah interesteing

no no no
nonnono

yes yes
--

I am wanting to read the file into an array spliting each element
by a blank line, such as @array[0] would be:

blah blah blah
blah blahblahblalh

Here is what I have tried doing:

open (LOG,"Messages.log"); while (<LOG>){ $string .= $_; } @array = split(/^\s*$/, $string);
and i have tried messing around with the regular expresion with no luck ..

Answer: Spliting a file into an array using blank lines as delimiter
contributed by davorg

The secret is to change $/ to the empty string. This will put Perl into 'paragraph mode', where the input record separator is one or more blank lines. Something like this:

#!/usr/bin/perl -w use strict; my @array; { local $/ = ''; @array = <DATA>; } print $array[0]; __END__ blah blah blah blah blahblahblalh oh yeah interesteing no no no nonnono yes yes
Answer: Spliting a file into an array using blank lines as delimiter
contributed by dsb

your code would work but the pattern you have '$string' splitting on is no good. Right now you have it matching any line with 0 or more whitespaces at the beginning. That's every line. Try this:

open (LOG,"Messages.log"); while (<LOG>){ $string .= $_; } @array = split(/\n{2,}/, $string); # note the new pattern

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2015-07-07 10:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (88 votes), past polls