Re: parsing a tree like structure



"Pat Rice" schreef:

I'm wondering if there is a nice way to parse this data, as in is
there any module that could handle this type of data, as in the was it
is presented? so that I can repeat is itn a tree like structure in
HTML ?

Maybe you are looking for something like this:

#!/usr/bin/perl -l
use strict; use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = $Data::Dumper::Indent = 1;

my @data = map [/^(\s+)\W+([\w\s]*\w)(?:\.+(.*))?/], <DATA>; # parse
$_->[0] = length $_->[0] for @data; # indent-width
0 and print Dumper(\@data); # for debugging

my (%tree, @keys);
for my $i (0 .. $#data) {
my $j = $i;
pop @keys while --$j >= 0 and $data[$i][0] < $data[$j][0];
pop @keys if $i > 0 and $data[$i][0] == $data[$i-1][0];
push @keys, $data[$i][1];
$tree{join "/", @keys} = $data[$i][2];
}
print Dumper(\%tree);

__DATA__
\==+Interface :
|----Link State.................................Down
\==+SCSI Interface :
|----Name....................................vmhba1
|----Console Name............................scsi1
|----Queue Depth.............................4096
\==+PCI Device :
|----Bus..................................0x0b
|----Slot.................................0x00
|----Function.............................0x00
\==+Scsi Stats :
|----Commands.............................48632378
|----Blocks Read..........................1862894689
|----Blocks Written.......................858120919
|----Aborts...............................0


which outputs:

$VAR1 = {
'Interface' => undef,
'Interface/Link State' => 'Down',
'Interface/SCSI Interface' => undef,
'Interface/SCSI Interface/Console Name' => 'scsi1',
'Interface/SCSI Interface/Name' => 'vmhba1',
'Interface/SCSI Interface/PCI Device' => undef,
'Interface/SCSI Interface/PCI Device/Bus' => '0x0b',
'Interface/SCSI Interface/PCI Device/Function' => '0x00',
'Interface/SCSI Interface/PCI Device/Slot' => '0x00',
'Interface/SCSI Interface/Queue Depth' => '4096',
'Interface/Scsi Stats' => undef,
'Interface/Scsi Stats/Aborts' => '0',
'Interface/Scsi Stats/Blocks Read' => '1862894689',
'Interface/Scsi Stats/Blocks Written' => '858120919',
'Interface/Scsi Stats/Commands' => '48632378'
};

--
Affijn, Ruud

"Gewoon is een tijger."

.



Relevant Pages

  • Re: [patch 0/8] Nesting class_device patches that actually work
    ... you give one of your "subclass devices" an interface, ... Take a step back and look what a kernel interface is about. ... device tree by following the "device" symlink from sysfs to get a unified tree ... the hierarchy is not a reason not to be able to walk up the chain and change ...
    (Linux-Kernel)
  • Re: ATL-COM: return pointer to instance
    ... > tree a second time. ... You should call AddRef any time you are handing a new reference to the ... both your own code and the VB code refer to the interface, ...
    (microsoft.public.vc.language)
  • Re: developing for something that isnt there..
    ... xml was chosen becouse well. ... creating a tree model of javabeans was my insisting becouse i didnt' ... had a complelty differnt strucutre and style of interface, ... and interface to send messages about them the classes ...
    (comp.lang.java.programmer)
  • Re: What I dont like about C# so far, compared to C++ (managed or
    ... locking type. ... program "using" the interface is show. ... probably never use it ("The IConfiguration interface defines ... Amazing that my home grown tree is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] Open Firmware device tree virtual filesystem
    ... the of_* routines continue to define the ... The main problem with the current powerpc interface to the device-tree ... tree is in kernel memory, and the lifetime of the properties is tied to ... In general, we don't fabricate the @unit-address part, we use OF's own ...
    (Linux-Kernel)