in reply to problems constructing an array of hashes

I think if you change:
use Data::Dumper; print Dumper(@image_array);
to
use Data::Dumper; print Dumper \@image_array;
You will see the type of structure you are aiming for.

On a seperate topic though since you are my %image outside the foreach block you will find that all the elements pushed onto @image_array will be a reference to the SAME hash. (also because just because $image is a hash_ref, apart from the similarities in the name it does not relate in any way to %image.)I doubt this is what you intend. From your second bit of code I would assume what you are looking for is something like:

#!/usr/bin/perl use warnings; use strict; use LWP::Simple; use LWP::UserAgent; use HTML::Treebuilder; use HTTP::Request; use Data::Dumper; my $raw_html = LWP::Simple::get("http://www.perlmonks.org") || die("$!\n"); my $tree = HTML::TreeBuilder->new; $tree->parse($raw_html); $tree->eof; my @image_array; foreach my $image ($tree->look_down("_tag", "img")){ push @image_array, $image; } print Dumper \@image_array;

update: Zaxo's solution above nicely skips the accomplishes the same population of the @image_array without the foreach loop.

-enlil

Replies are listed 'Best First'.
Re: Re: problems constructing an array of hashes
by Anonymous Monk on May 13, 2004 at 18:21 UTC
    Thanks all! Here is what I am using now. Works like a champ :)
    #!/usr/bin/perl use warnings; use strict; use LWP::Simple qw(); use LWP::UserAgent; use HTML::Treebuilder; use HTTP::Request; use Data::Dumper; my $raw_html = LWP::Simple::get("http://www.perlmonks.org") || die + ("$!\n"); # print $raw_html; my $tree = HTML::TreeBuilder->new; $tree->parse($raw_html); $tree->eof; #$tree->dump; my @image_array = ($tree->look_down("_tag", "img")); foreach my $img (@image_array){ print "image src $img->{'src'}\n"; print "image width $img->{'width'}\n"; }