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"; # } } }
My html document is here

<!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?


In reply to accessing a hash via reference by richill

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.