in reply to Parsing HTML into various files
You have nine groups of seven rows so we process seven rows at a time loading the data into an AoH. I think it reads fairly well and maintaining it ought to be relatively straight forward if your HTML changes.
extract and the note field shortened for brevity:#! /usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::TreeBuilder; my $file_name = q{la.html}; my $t = HTML::TreeBuilder->new_from_file($file_name) or die qq{cant build tree from *$file_name*: $!}; my @trs = $t->look_down(_tag => q{tr}); my @db; while (@trs){ my @fields = splice(@trs, 0, 7); my %rec; $rec{group} = $fields[0]->as_text; $rec{type} = $fields[1]->as_text; my @tds; @tds = $fields[2]->look_down(_tag => q{td}); $rec{level} = $tds[1]->as_text; for my $field (3..5){ @tds = $fields[$field]->look_down(_tag => q{td}); $rec{$tds[0]->as_text} = $tds[1]->as_text; $rec{$tds[2]->as_text} = $tds[3]->as_text; } $rec{note} = $fields[6]->as_text; push @db, \%rec; } print Dumper \@db;
Oh, and btw, 38 lines. :-)$VAR1 = [ { 'Saving Throw:' => 'None', 'Casting Time:' => '1', 'Area of Effect:' => ' 10 ft.×10 ft./level path', 'Range:' => 'Touch', 'Duration:' => '3 rds. + 2 rds./level', 'note' => ' By means of this spell... or mica.', 'Components:' => 'V, S, M', 'group' => 'Detect Illusion', 'level' => '1', 'type' => '(Divination)(Mentalism)' }, { 'Saving Throw:' => 'Special', 'Casting Time:' => 'Special', 'Area of Effect:' => 'Script reader', 'Range:' => 'Touch', 'Duration:' => '1 day/level', 'note' => ' This spell enables the ....', 'Components:' => 'V, S, M', 'group' => 'Illusionary Script', 'level' => '3', 'type' => '(Illusion/Phantasm)' }, # ... <snipped> ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing HTML into various files
by Lady_Aleena (Priest) on Aug 25, 2010 at 18:25 UTC | |
by wfsp (Abbot) on Aug 26, 2010 at 09:25 UTC |