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

recursive algorithm for nested data structures

by Anonymous Monk
on Aug 19, 2009 at 19:01 UTC ( [id://789926]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody I have a program with an output like this:

(root (class living_thing (spec human (prop has_legs)) (spec bird (prop has_wings))))
I'd like to read it into array in arrays but the problems are: 1. I dont know the structures depht (which is variable) 2. I dont know how to retrieve the information once stored because of 1. Any ideas?

Replies are listed 'Best First'.
Re: recursive algorithm for nested data structures
by ikegami (Patriarch) on Aug 19, 2009 at 19:36 UTC
    Parse::RecDescent grammar:
    my $grammar = <<'__EOI__' { use strict; use warnings; } parse : list /\Z/ { $item[1] } list : '(' list_ele(s?) ')' { $item[2] } list_ele : list | TERM TERM : /\w+/ __EOI__
Re: recursive algorithm for nested data structures
by JavaFan (Canon) on Aug 19, 2009 at 20:54 UTC
    #!/usr/bin/perl use 5.010; use strict; use warnings; use YAML; use Regexp::Grammars; my $str = <<'EOT'; (root (class living_thing (spec human (prop has_legs)) (spec bird (prop has_wings)))) EOT my $parser = qr{ ^ <List> $ <rule: List> (?: \( <[MATCH=Atom]>* \) ) <rule: Atom> <MATCH=List> | <MATCH=Word> <token: Word> \w+ }x; print Dump $/{List} if $str =~ /$parser/; __END__ --- - root - - class - living_thing - - spec - human - - prop - has_legs - - spec - bird - - prop - has_wings
Re: recursive algorithm for nested data structures
by tilly (Archbishop) on Aug 19, 2009 at 20:46 UTC
    Let me get this straight. You want to turn an arbitrary tree of data (which happens to be written in Lisp) into an array of arrays?

    I sense an XY Problem here. What you need to do is instead of deciding your data structure (array of arrays), you need to look through the data you're given, the questions you're going to need to answer, and then try to come up with something that works.

    I have only one sample of your input and I don't know what you're going to be asked, so I can't make good suggestions. However I can say that fitting an arbitrary tree into an array of arrays is a bad idea. "OTOH is there some uniformity that you haven't discussed? For instance are the only types of nodes root, class, spec, and prop in arrangements structured like your example? Could AI::Prolog be helpful for you?

    Without knowing context I can't even tell whether those are useful questions to ask!

Re: recursive algorithm for nested data structures
by rowdog (Curate) on Aug 20, 2009 at 15:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://789926]
Approved by lostjimmy
Front-paged by snoopy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found