in reply to Convert delimited string into nested data structure

I think you might be better off with a hash of hashes, e.g.
%foo = (one => 1, foo => { bar => 1, # etc. } );
but in any case, could you post what you've tried so far?

Replies are listed 'Best First'.
Re^2: Convert delimited string into nested data structure
by ikegami (Patriarch) on Feb 19, 2007 at 18:50 UTC
    use strict; use warnings; use Data::Dumper qw( Dumper ); my %tree; while (<DATA>) { chomp; my $p = \%tree; foreach (split(qr{\s*/\s*}, $_)) { $p = $p->{$_} ||= {}; } } print Dumper \%tree; __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five

      A little recursive sub to compile this into the right format:

      use strict; use warnings; use Data::Dumper qw( Dumper ); my %tree; while (<DATA>) { chomp; my $p = \%tree; foreach (split(qr{\s*/\s*}, $_)) { $p = $p->{$_} ||= {}; } } sub re_format { my $data = shift; my $out = []; for my $key (keys %$data) { my $temp = { name => $key}; if (keys %{ $data->{$key} }) { $temp->{children} = re_format($data->{$key}) } push @$out, $temp; } return $out; } print Dumper('First Step' => \%tree, 'Final' => re_format(\%tree)); __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five

      ___________
      Eric Hodges
        Thank you very much Eric.
        No ... I spoke too soon again. The reformatted data is now out of order. The reason I use a List of Hashes is because I have to preserve the same order.

        In order words, %tree really needs to be @tree.

        Can anybody help me? I have spent far too much time trying to solve this problem. :(
      Are you suggesting that with a few modifications that I can use your code snippet to achieve my goal? Because I don't see how I can push the children onto their parents using your code (which doesn't produce the output I need).

      But thanks anyways -- good ideas in there.

        Are you suggesting that with a few modifications that I can use your code snippet to achieve my goal? Because I don't see how I can push the children onto their parents using your code (which doesn't produce the output I need).

        Yes, this kind of question has been discussed before. You may also be interested in nodes referenced in that thread. Two modules, that were mentioned in the discussion and that could come useful to make "structured text" into a complex data structure are Data::Hierarchy and Data::Diver.

Re^2: Convert delimited string into nested data structure
by Anonymous Monk on Feb 19, 2007 at 18:49 UTC
    Well, I am quite scatter-brained at the moment. Under more ideal circumstances, I would "sleep on this problem" and solve it in the morning -- but I have to get this part done ASAP so I can continue with the larger task at hand.

    As such I keep tearing down my past efforts -- but just so you know I am working on this instead of just waiting for others to do my work for me ...

    sub recurse { my ($ref, $car, @cdr) = @_; if ($prev_car eq $car) { my $hash = { name => $car, children => [] }; push @$ref, $hash; return unless @cdr; $prev_car = $car; recurse($hash->{children}, @cdr); } push @$ref, { name => $car, children => [] }; return unless @cdr; $prev_car = $car; recurse($ref, @cdr); }
    Which produces the following output: I forgot to say "Thanks in advance" too. :)
      If your keys are well-behaved, then this will do the trick:
      while (<>) { chomp; s!\s*/\s*!}{!g; eval "\$h\{$_\} = 1;"; }
        A reply falls below the community's threshold of quality. You may see it by logging in.
        A reply falls below the community's threshold of quality. You may see it by logging in.