Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Graph File Parsing

by broquaint (Abbot)
on Mar 10, 2003 at 14:40 UTC ( [id://241727]=note: print w/replies, xml ) Need Help??


in reply to Graph File Parsing

Here's a basic parser for your example graph
use strict; use Data::Dumper; use Regexp::Common; my $name = qr/[a-z0-9_]+/i; my $val = qr/\S+/; my $braces = qr/$RE{balanced}{-parens=>'{}'}/; my $node = qr/($name) \s+ ($braces)/x; my $assign = qr/($name) [=:] \s* ($val) (?:\s+ | })/x; my $graph = <<TXT; graph { node { title: "node1" loc {x: 10 y: 20} } node { title: "node2" loc { x: 10 y: 20 } } edge { sourcename="node1" targetname="node2" } } TXT print Dumper( parse_graph($graph) ); sub parse_graph { my $graph = shift; my $tree = {}; pos($graph) = 0; PARSE: { if($graph =~ /\G {? \s* $assign/sgcx) { $tree->{$1} = $2; redo PARSE; } elsif($graph =~ /\G .*? $node/sgcx) { push @{ $tree->{$1} }, parse_graph($2); redo PARSE; } } return $tree; } __output__ $VAR1 = { 'graph' => [ { 'edge' => [ { 'sourcename' => '"node1"', 'targetname' => '"node2"' } ], 'node' => [ { 'title' => '"node1"', 'loc' => [ { 'x' => '10', 'y' => '20' } ] }, { 'title' => '"node2"', 'loc' => [ { 'x' => '10', 'y' => '20' } ] } ] } ] };
Will probably need tweaking for your own preferences but hopefully it's a start :)
HTH

_________
broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found