in reply to Re^3: Parsing HTML into various files
in thread Parsing HTML into various files

ACK! It was the file name, I had a small typo in it that I didn't catch earlier. So now I ran it, but there are still some issues which I can't pin down.

The errors and output

Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 17. Odd number of elements in anonymous hash at C:\..\perl\tokeparser.pl l +ine 51. $VAR1 = { 'Saving Throw:' => { 'None' => undef }, 'Casting Time:' => { '2' => undef }, 'Area of Effect:' => { '10 yds./level' => undef }, 'Range:' => { '0' => undef }, 'Duration:' => { '5 rds./level' => undef }, 'Level:' => { '2' => undef }, 'Components:' => { 'V, S, M' => undef } };

line 17

$hash{$key} = { @table };

line 51

$hash{$key} = { @table };

I still haven't had that A-HA! moment where I get how this works.

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^5: Parsing HTML into various files
by bart (Canon) on Aug 25, 2010 at 18:43 UTC
    It looks to me that the HTML of this HTML file is not compatible with the file I used to develop this code. Can I take a look at that file?

    I suspect that, by contrast to the other file, here you used "th" tags to indicate the attribute names, instead of a combination of "td" and "b" tags.

    Anyway, if that is indeed the case: don't panic. The parser can probably be tweaked to handle this, possibly by looking at the "colspan" attribute.

      bart, you got it in 1. I totally forgot that the files that are online are completely different from the local files which I have structured a lot differently. I will just download the old files since the new ones are so very different. I took a look at the underlying code of the files that are online about 10 minutes ago right before I updated my reply to wfsp.

      Update: After getting the right files, I ran your script. It worked almost flawlessly, except that there are a nested tables in some of the descriptions that are throwing this off. I am looking at the following lines thinking that something should be added there to take into account the nested tables. When colspan = "4" ignore the tables within it.

      if($flag == 1) { $_ = ""; my $colspan = $token->get_attr('colspan'); if($colspan) { push @table, $colspan == 2 ? 'school(s)' : 'description'; } }
      Have a cookie and a very nice day!
      Lady Aleena
Re^5: Parsing HTML into various files
by psini (Deacon) on Aug 25, 2010 at 18:09 UTC

    I don't know what you were trying to do, but { @table } IS an anonymous hash with an odd number (1) of elements.

    Maybe you intended

    $hash{$key} = \@table;

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      I don't know what you were trying to do, but { @table } IS an anonymous hash with an odd number (1) of elements.
      Only if @table contains an odd number of elements. That implies there's something fishy with her HTML.

      The HTML I used as an example consists of a table with 4 columns, where column 1 and 3 are attribute names (to be used as hash keys) and columns 2 and 4 are attribute values (to be used as hash values).

      Maybe you intended
      $hash{$key} = \@table;
      No, if she did that, every hash value would be a reference to the same hash.

      The intention is to turn the current list of name/value pairs into a hash. Something like:

      @table = ( foo => 10, bar => 20 ); $hashref = { @table };