richill has asked for the wisdom of the Perl Monks concerning the following question:
Hi, Im trying to find the value of the hash keys of the token returned when HTML::Tokeparser operates on a HTML tag.
I understand that references dont work as hash keys. Im 70% sure I'm doing something different.
I think Im running into problems becasue I am not dereferencing properly.
I get the error message
Reference found where even-sized list expected at example.pl line 28
I want to access the data stored in HASH(0x86b44c) using the code below
#!/usr/bin/perl # script to find the hash keys of a token returned by HTML:tokeparser. # for example <li><a href="link1.html" class="c1">link 1</a></li> # returnes href and class. use strict; use warnings; use HTML::Tokeparser; use Data::Dumper; my $stream = HTML::TokeParser->new("test.html") || die "couldn't read in HTML file: $! "; $stream->get_tag("h1"); $stream->get_tag("ul"); while ( my $token = $stream->get_token ) { if ( $token->[0] eq 'S' and $token->[1] eq 'a' ) { # what is going on here? What am i dealing with? print " $token->[2]\n "; # print " $token->[2]{href}\n\n "; # print Dumper "$token \n\n\n" # set the reference to the hash to new hash %hash # how do i dereference this? my %hash = $token->[2]; # below wont work unless i feed it a hash # my $key; # foreach $key (keys %hash) { # print "at $key we have $hash{$key}\n"; # } } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test document</title> </head> <body> <h1> This is a test of HTML::Tokeparser</h1> <ul> <li><a href="link1.html" class="c1">link 1</a></li> <li><a href="link2.html" class="c1">link 2</a></li> <li><a href="link3.html"class="c1">link 3</a></li> <li><a href="link4.html" class="c1">link 4</a></li> <li><a href="link5.html"class="c0">link 5</a></li> <li><a href="link6.html" class="c0">link 6</a></li> </ul> </body> </html>
What am I doing wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: accessing a hash via reference
by davidrw (Prior) on Sep 03, 2006 at 12:35 UTC | |
by richill (Monk) on Sep 03, 2006 at 13:30 UTC | |
by Fletch (Bishop) on Sep 03, 2006 at 13:56 UTC | |
by richill (Monk) on Sep 03, 2006 at 14:09 UTC |