in reply to So you think you're good with structures?

Ok. After annoyingly hard work, I've got the following code that works! Now, obviously, it works on the data structure you've given and returns the output you desired. I make no anythings when it comes to any other data structures.

my @tables_seen; sub traverse_nodes { my ($db, $start, $record) = @_; push @tables_seen, $start; my @records = ("$record"); CHILD: foreach my child (keys %{$db->{$start}{$record}{children}}) { next CHILD if grep /$child/, @tables_seen; my @child_values; foreach my $key (keys %{$db->{$start}{$record}{children}{$chil +d}}) { push @child_values, traverse_nodes($db, $child, $key); } my @temp = @records; @records = (); foreach my $rec (@temp) { push @records, map { "$rec $_" } @child_values; } } return @records; } my $starter = 'table a'; my @good_records; foreach my $record (keys %{$rdb->{$starter}}) { @tables_seen = (); push @good_records, map { join "", $_ } traverse_nodes($rdb, $starter, $record); } print "REC: $_\n" foreach @good_records;

------
/me is the brightest bulb in the chandelier!

Replies are listed 'Best First'.
Re: Q: So you think you're good with structures? A: Umm... Yes!
by clintp (Curate) on Aug 04, 2001 at 04:13 UTC
    Wonderful. This actually seems to work with a couple of sample datasets that I had laying around. I'll try it with real data from the application on Monday (I'm home now and all of that stuff's at work). A few minor modifications and I can put it in production code.

    I don't see anything wrong with it. (And I see one of my mistakes, *DOH*. I hate mental blocks.) I'll let you know how it works on Monday. Thanks!

    PS: does anyone have a need for an ad-hoc report writer? Outputs in Excel, PDF, text, HTML given some XML input and massaging... :)

      Yes! I could definitely use an ad-hoc report writer!!! Email it to dragonchild93@yahoo.com as soon as you can. :)

      That'll be your payment. *grins*

      ------
      /me wants to be the brightest bulb in the chandelier!

Re: Q: So you think you're good with structures? A: Umm... Yes!
by clintp (Curate) on Aug 06, 2001 at 22:04 UTC
    Okay, I found a structure that doesn't get processed by the code well. On inspection, it looks like it should though. It follows shortly...It's just a Data::Dumper dump, so it might be a bit hard to read.
      Ooops. In the recursive function, change the last few lines to:
      pop @tables_seens; return @records; }
      This allows you to remove the @tables_seens = () line in the control section.

      I am sorry about forgetting that. I thought about doing a pop, but the dataset you gave at first didn't find that bug. *blushes*

      ------
      /me wants to be the brightest bulb in the chandelier!