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"; # } } }
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?

Replies are listed 'Best First'.
Re: accessing a hash via reference
by davidrw (Prior) on Sep 03, 2006 at 12:35 UTC
    References: perlreftut, perldsc
    # what is going on here? What am i dealing with? print " $token->[2]\n "; # how do i dereference this?
    use Data::Dumper; print Dumper $token->[2]; # or w/o Data::Dumper print join(":", keys %{$token->[2]}), "\n";
    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 $hash = $token->[2]; foreach my $key (keys %$hash) { printf "at %s we have %s\n", $key, $hash->{$key}; }
    Or, alternatively (but the above is better, because this way below makes a copy of the data in the hash):
    my %hash = %{$token->[2]}; foreach my $key (keys %hash) { printf "at %s we have %s\n", $key, $hash{$key}; }
      Thank you. Thats great.Just one question

      Is the difference between

           my %hash = %{$token->[2]}; and

      my %hash = $token->[2];

      The former trys to assign a string to a hash. Saying something like %hash = HASH(0x86b258)

      The latter assigns a hash to a hash by telling perl that whatever is inside %{} is definately a hash.Perl then gos and gets whatever $token->[2] points to and dereferences it.

        Need for <code></code> tags aside, the first is dereferencing a hash ref into a list and then assigning that list to your hash. The second is attempting to assign the hashref to the hash, which will get you a gripe about assigning an odd number of elements to a hash and a hash with one entry with a stringified representation of the hashref (what you're seeing) as the key.