Re: parsing a tree like structure
- From: yitzle@xxxxxxxxxxxxxxxxxxxxx (Yitzle)
- Date: Thu, 26 Jun 2008 13:12:38 -0400
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 );
.
- References:
- parsing a tree like structure
- From: Pat Rice
- parsing a tree like structure
- Prev by Date: Re: parsing a tree like structure
- Next by Date: Re: How to remove trailing commas and points from a CSV file ?
- Previous by thread: Re: parsing a tree like structure
- Next by thread: Re: parsing a tree like structure
- Index(es):
Relevant Pages
|