For comparison, this uses HTML::TreeBuilder.

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.

#! /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;
extract and the note field shortened for brevity:
$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> ];
Oh, and btw, 38 lines. :-)

In reply to Re: Parsing HTML into various files by wfsp
in thread Parsing HTML into various files by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.