Re: parsing a tree like structure
- From: rvtol+news@xxxxxxxxxxxx (Dr.Ruud)
- Date: Thu, 26 Jun 2008 21:54:27 +0200
"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."
.
- References:
- parsing a tree like structure
- From: Pat Rice
- parsing a tree like structure
- Prev by Date: Why doesn't LWP get html file?
- Next by Date: Re: How to remove [], {}, and other characters, rendering numeric values in a CSV file ?
- Previous by thread: Re: parsing a tree like structure
- Next by thread: how to click span link in Win32::IEAutomation
- Index(es):
Relevant Pages
|