Sorry for the multiple replies... This code works. Though I think I
might be doing something "bad". When I comment out the line:
    $curNode->{ $nodeName }{ '__PARENT__' } = $curNode;
after the loop, %root is an empty hash. I'm not sure why.

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

open my $FH, "< t" or die;

my %root = ();
my $depth = 0;
my $curNode = \%root;

while ( my $line = <$FH> )
{
    chomp $line;
    $line =~ /^(\s*)/;
    my $leadingSpace = length $1;

    if ( $leadingSpace < $depth ) {
        $curNode = $curNode->{ '__PARENT__' };
    }

    if ( $line =~ /^\s*\\==\+(.*) :$/ ) {
        my $nodeName = $1;
        $curNode->{ $nodeName }{ '__PARENT__' } = $curNode;
        $curNode = $curNode->{ $nodeName };
    } elsif ( $line  =~ /^\s+\|----([^.]+)\.+([^.]+)$/ ) {
        $curNode->{ $1 } = $2;
    }

    $depth = $leadingSpace;
}

print Dumper ( \%root );

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to