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

Quick question, does use strict; and putting in my %hash; change the basic makeup of the script? I am getting the following error after those two changes:

Can't call method "get_token" on an undefined value at C:\Documents an +d Settings\ME\My Documents\fantasy\files\perl\parser.pl line 11.

Line 11 in my copy of the script is $parser->get_token('table'); Yes, I expanded the variables in the script. :)

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Parsing HTML into various files
by psini (Deacon) on Aug 25, 2010 at 08:48 UTC

    From the error message it looks like $parser is undef. So it is probably the previous line

    my $parser = HTML::TokeParser::Simple->new(...);
    which fails. Check if $parser is defined, and if the filename is valid; I don't think that HTML::TokeParser::Simple->new returns an error message, so best chance is that the file name is invalid.

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

Re^3: Parsing HTML into various files
by bart (Canon) on Aug 25, 2010 at 10:00 UTC
    psini is absolutely right, adding use strict and declaring all variables will not change the working of script at all.

    So the only explanation I can think of is that it can't read the file. BTW in my case I downloaded the file from the URL and put it right next to the script. Did you forget that? If the file is elsewhere, you have to adjust the file path.

      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
        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.

        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."