Re: parsing a tree like structure



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 );
.



Relevant Pages

  • PART SUMMARY: cannot change user password
    ... I'm not running NIS or rpc.yppasswd, but from the various suggestions I was ... 'passwd -r files "user_account"' as root, ... Any replies are much appreciated. ...
    (SunManagers)
  • Re: os.walk bug?
    ... Each iteration of os.walk returns three parameters; one of those, dirs, ... When looping in a for loop, it is generally good programming practice to ... Root contains the current path being visited, ...
    (comp.lang.python)
  • Re: C++ if statements - only last one is ever true (code)
    ... root wrote: ... > Thanks for all your replies so far. ... > I can't post the entire block of code here for commercial reasons, ... > selection of the if statements are: ...
    (comp.lang.cpp)
  • [PATCH 007 of 8] knfsd: exportfs: add find_disconnected_root helper
    ... Break the loop that finds the root of a disconnected subtree ... * Find root of a disconnected subtree and return a reference to it. ...
    (Linux-Kernel)
  • Re: C++ if statements - only last one is ever true (code)
    ... "root" wrote in message ... Thanks for all your replies so far. ... I can't post the entire block of code here for commercial reasons, ... selection of the if statements are: ...
    (comp.lang.cpp)