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

Hi everyone, I need a strategy for something to do in perl. I have an input file of the following format:
a;
a;b;
a;b;e;
a;b;f;
a;c;
a;c;g;
a;c;h;
a;c;h;i;
a;c;h;j;
a;d;
And I need to parse this to get the following (it is a Newick tree format):
((e,f)b,(g,(i,j)h)c,d)a
The parent nodes of this "tree" are after the brackets in which the children are!
Anyone has a strategy to suggest?
Thanks, Have a Happy New Year!

Replies are listed 'Best First'.
Re: I need a strategy please!
by BrowserUk (Patriarch) on Jan 04, 2010 at 06:53 UTC

    Fun!

    #! perl -slw use strict; use Data::Dump qw[ pp ]; sub x; sub x{ my $ref = shift; my @keys = sort keys %{ $ref }; return unless @keys; return join ',', map { my $leafs = x( $ref->{ $_ } ); $leafs ? "($leafs)$_" : $_ } @keys; } my %tree; while( <DATA> ) { chomp; my $key; my $ref = \%tree; my @nodes = split ';', $_; $key = shift @nodes, $ref = $ref->{ $key } //= {} while @nodes; } pp \%tree; print x( \%tree ); __DATA__ a; a;b; a;b;e; a;b;f; a;c; a;c;g; a;c;h; a;c;h;i; a;c;h;j; a;d;

    Outputs

    C:\test>815486 { a => { b => { e => {}, f => {} }, c => { g => {}, h => { i => {}, j => {} } }, d => {}, }, } ((e,f)b,(g,(i,j)h)c,d)a

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: I need a strategy please!
by desemondo (Hermit) on Jan 04, 2010 at 01:36 UTC
      strategy
      1. open file
      2. read line by line
      3. split line into values, and stuff into Tree object
      4. close file
      5. walk tree and output into desired format
      6. ...
      7. ...
      8. profit :)
Re: I need a strategy please!
by NateTut (Deacon) on Jan 06, 2010 at 19:49 UTC