in reply to Best way to parse multiline data?

Don't slurp 600M - that translates to roughly 3G+ of RAM. Not a good plan, to say the least.

Read it line by line, but keep track of which section you're in and if the section hasn't change, just concatenate. Then, when the section changes, go ahead and process the last section, then set the current section to the new one.

my ($section, @value); while (<FILE>) { chomp; my ( $temp_section, $temp_value ) = /^(?:([^:]+):)?\s*(.*)/; if ( $temp_section ) { if ( $section ) { # Process the old value somehow. } $section = $temp_section; @value = ( $temp_value ); } else { push @value, $temp_value; } }

Replies are listed 'Best First'.
Re^2: Best way to parse multiline data?
by BrowserUk (Patriarch) on Apr 16, 2005 at 03:39 UTC
    Don't slurp 600M - that translates to roughly 3G+ of RAM.

    Not if you slurp to a scalar.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
Re^2: Best way to parse multiline data?
by jalewis2 (Monk) on Apr 16, 2005 at 03:16 UTC
    I hadn't considered slurping for that exact reason. The regex is what gave me some ideas. I've never done read ahead regexes. I figured someone here at PM would steer me in the right direction.

    You sample code is exactly what I am looking for. Thanks!