Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: String parsing

by Zaxo (Archbishop)
on Jan 05, 2004 at 10:54 UTC ( [id://318807]=note: print w/replies, xml ) Need Help??


in reply to String parsing

Your data lines are different enough that each type needs its own parser. A hash of coderefs ("dispatch table") will do that nicely,

my $parser = { OFF => sub {()}, SUCCESS => sub { local $_ = shift; /\((\w*)\)/ }, # call these in list context! ERROR1 => sub { local $_ = shift; /disk number \((\d+)\) at \((\d+:\d+)\)/; }, WARNING1 => sub { local $_ = shift; /(\w.*)^/; } }; sub parse_line { local $_ = shift; my ($key, $data) = split ':', $_, 2; $key =~ tr/()//d; ($key, $parser->{$key}->($data)); }
parse_line() should be called in list context, too.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: String parsing
by Abigail-II (Bishop) on Jan 05, 2004 at 12:35 UTC
    Your data lines are different enough that each type needs its own parser.
    Uhm, no, as shown in several other replies.
    A hash of coderefs ("dispatch table") will do that nicely,
    Actually, your solution is very inflexible. It can't even deal with:
    WARNING(2): system is rebooting
    (only the exit value is different from the original). It'll produce an error, as Perl will try to use an undefined value as a code reference.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://318807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-18 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found