Re: parsing a tree like structure



This code is a start. It needs some playing with, still.

#!/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 ( $line =~ /^\s+\\==\+(.*) :$/ ) {
my $nodeName = $1;
$curNode->{ $nodeName }{ '__PARENT__' } = $curNode;
$curNode = $curNode->{ $nodeName };
} elsif ( $line =~ /^\s+\|----([^.]+)\.+([^.]+)$/ ) {
$curNode->{ $1 } = $2;
}
}

print Dumper ( \%root );
.



Relevant Pages

  • Re: cat (.sh) in Perl
    ... use warnings; ... use strict; ... Where is $root defined and what does it contain? ... You should *ALWAYS* verify that the file opened correctly. ...
    (perl.beginners)
  • Re: Sharing variables between modules
    ... use strict; ... use warnings; ... # Note that this must also come *before* the use Foo line. ... package Foo; ...
    (comp.lang.perl.misc)
  • Re: Dynamic directory handles?
    ... > 1) You should always enable warnings (and strict) when ... > perldoc -q filehandle ... >> sub dircount { ... > use warnings; ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... If you had enabled the warnings and strict pragmas then perl would have ... foreach my $logfile { ...
    (perl.beginners)
  • Re: Calculating values of a 2d array by comparison of 2 strings
    ... use strict; ... use warnings; ... KMGYILGKGL GKNEDGIKIP VEAKINQKRE GIGYPF '; ... # get the shortest sequence length;-) ...
    (comp.lang.perl.misc)