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

Hello Monks,

Once again I come seeking your wisdom. I am trying to traverse intermixed hash and array references. Here's the code I have so far.
while(my ($key, $value) = each(%{$tree})) { if (ref($value) eq 'ARRAY') { foreach my $array (@{$value}) { my @split = split(/\=/, $array); if (ref($split[1]) eq 'HASH') { print "hello"; while(my ($key, $value) = each(%{$split[1]})) { print "$key = $value<br>\n"; } } } } }
The problem is that $array = 'HTML::Element=HASH(0x1a031f4)' a scalar reference. And when I split it its still a scalar reference.

What I am trying to do is spider an intranet and move the relevant files (big mess). If anyone knows of a better way to do this, I'm using HTML::TreeBuilder to extract the links, then I'm all ears.
Thanks again in advance!

Replies are listed 'Best First'.
Re: Trouble Traversing Hash and Array References
by ccn (Vicar) on Aug 23, 2004 at 14:58 UTC

    You can not dereference a string "HASH(0x1a031f4)" because it is not a reference. Your $array is a reference to an object HTML::Element, so use it's methods

    while(my ($key, $value) = each(%{$tree})) { if (ref($value) eq 'ARRAY') { foreach my $array (@{$value}) { for (@{ $array->extract_links('a', 'img') }) { my($link, $element, $attr, $tag) = @$_; print "Hey, there's a $tag that links to ", $link, ", in its $attr attribute, at ", $element->address(), ".\n"; } } } }
    see HTML::Element

    Update: misprints fixed

      Thats exactly what I was looking for. Thank you!
Re: Trouble Traversing Hash and Array References
by eric256 (Parson) on Aug 23, 2004 at 14:52 UTC

    Thats an object. Try looking on the cpan documentation for HTML::Element and see if it has the info you need. I'm not sure how you ended up with files in and HTML tree object though.


    ___________
    Eric Hodges