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

Re^2: Design hints for a file processor

by PhilHibbs (Hermit)
on Jul 07, 2008 at 12:18 UTC ( #695961=note: print w/replies, xml ) Need Help??

in reply to Re: Design hints for a file processor
in thread Design hints for a file processor

Yes, that's exactly what I do currently - the script is basically a whole load of special cases with no real structure to it. Well, this is my actual code for that:

$cat = $1 if /^\s+Category "(.+)"/;

I prefer this notation, I know some people don't.

Replies are listed 'Best First'.
Re^3: Design hints for a file processor
by moritz (Cardinal) on Jul 07, 2008 at 12:32 UTC
    If you want structure, use a real parser. Here is one, albeit a bit hacked up:

    It returns a sort of parse tree with an array ref for each block or line, where blocks look like ['BLOCK', $name_of_block, @lines_in_this_block] and lines look like ['LINE', $key, $value].

    Depending on your exact data format and what you want to extract, hashes might be more suitable.

      The file is up to half a gigabyte, I'm not keeping all that in memory. I could split it up by job, I suppose.
        You don't have to keep it all in memory. My parser uses just one line of lookahead, you can easily refactor the shift @lines; and $lines[0] into subs that work on a file handle.
        { my $line = <DATA>; chomp $line; $line =~ s/^\s+//: # handles '$lines[0]' sub peek { return $line; } # handles 'shift @lines' sub next_line { my $tmp = $line; $line = <DATA>; chomp $line; $lines =~ s/^\s+//; return $tmp; } # handles boolean check for @lines sub is_exhausted { return !defined $line } }

        I didn't test it, but it should work along these lines.

        Instead of nitpicking details, think on the overall architecture and fix small issues for yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695961]
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-01-20 03:45 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.