scalar / hash problem in HTML::Parser



Hi all,

I'm using HTML::Parser to process files containing snippets of html
looking like:
<option value="1">First option</option><option value="2">Another
choice</option><option value="4">pick me</option>

I'm wanting to create a hash of option value, name pairs. Ie, 1 =>
"First option",2=>"Another choice" and so on. Problem is, I don't know
how to handle the hash returned in $attr in the start sub:

#!/usr/bin/perl -wT
use strict;
use HTML::Parser;

my %choices;
my $file = 'test_snippet';
my $parser = HTML::Parser-> new(api_version => 3,
start_h => [\&start, "tagname, attr"],
);

sub start {
my ($tag, $attr) = @_;
print "$tag\t$attr\n";
# do something here to read the hash returned in $attr
# and add the key value pair to %choices but what?
}
$parser->parse_file($file) or die "couldn't parse file";


I realise it should be obvious and simple, but I feel like I've been hit
with the stupid stick for now. Any suggestions?

Thanks,
Tim Bowden

.



Relevant Pages

  • Re: scalar / hash problem in HTML::Parser
    ... I'm using HTML::Parser to process files containing snippets of html ... I'm wanting to create a hash of option value, ... how to handle the hash returned in $attr in the start sub: ...
    (perl.beginners)
  • Re: looking for help with a counting algorithm
    ... >> subcategory is counted, the code goes back up the tree to the root, adding ... >> involve retrieving all the category memberships from the database, ... sub ReadCategories{ ... ReadCategories is called with two empty hash pointers by any of the ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... >>of instance data. ... >>sub rtnError { ... its a reference to a hash. ... functions in the package to see it. ...
    (comp.lang.perl.misc)
  • Re: writing get_script as an external routine callable by C
    ... which is close as I'm going to able to come to getting them into a tree. ... a hash has some of the properties of a binary tree: ... sub configuration { ... and its corresponding value will be $script. ...
    (comp.lang.perl.misc)
  • Re: Any suggestions for my programming style?
    ... Also, add "use strict". ... Better use a lexical hash instead of the package variables ... > sub get_century_code ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)