uni_j has asked for the wisdom of the Perl Monks concerning the following question:

I wonder why variable '$url' and '$url_desc' won't pass information into my hash ('%url_dep'). I'd like to make a hash list with information that are stored in the variables.
use HTML::TokeParser; use WWW::Mechanize; use Data::Dumper; my $mech = WWW::Mechanize->new(); my $html; my $url = "http://ug.cal.dal.ca/"; my %url_dep; start(); sub start{ $mech->get($url); $html=$mech->content(); my $stream = HTML::TokeParser->new(\$html); while(my $token = $stream->get_token()){ if($token->[1] eq "div" && $token->[2]{id} eq "content-zone5"){ while(my $token = $stream->get_token("a")){ my $url_desc = $stream->get_phrase(); my $url = $token->[2]{href}; $url_dep { $url } = $url_desc; } } } print Dumper \%url_dep; foreach (sort { $a <=> $b } keys(%url_dep) ){ my $dep_url = $a; my $dep_name = $b; print $dep_url; } }

Replies are listed 'Best First'.
Re: Hash debugging.
by ikegami (Patriarch) on May 11, 2009 at 14:44 UTC
    for doesn't set $a and $b. The key is in $_, the value is in $url_dep{$_}.
Re: Hash debugging.
by almut (Canon) on May 11, 2009 at 14:36 UTC
    I wonder why variable '$url' and '$url_desc' won't pass information into my hash

    So have you verified that the while loop wherein you put things into the hash is in fact being executed? And that $url_desc and $url do contain what you expect them to contain?  What's the Data::Dumper output?

Re: Hash debugging.
by cnhackTNT (Novice) on May 11, 2009 at 15:05 UTC
    I think:
    ----
    while(my $token = $stream->get_token("a"))

    should be:
    while(my $token = $stream->get_tag("a"))

    and you should change:
    my $url_desc = $stream->get_phrase();

    to:
    my $url_desc = $stream->get_text();

    also change:
    my $url = $token->[2]{href};

    to:
    my $url = $token->[1]{href};
    if this is what you want. :-)