I am having problems constructing an array of hashes. I was trying to use this example - 1976 but I am doing something incorrectly. Here is some test code.
#!/usr/bin/perl use warnings; use strict; use LWP::Simple qw(); use LWP::UserAgent; use HTML::Treebuilder; use HTTP::Request; 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=(); my %image = (); foreach my $image ($tree->look_down("_tag", "img")){ push (@image_array, \%image); } use Data::Dumper; print Dumper(@image_array);
This outputs:
$VAR1 = {}; $VAR2 = $VAR1; $VAR3 = $VAR1; $VAR4 = $VAR1;
If I modify the original code to this you can see it outputs an array for each image in the html:
#!/usr/bin/perl use warnings; use strict; use LWP::Simple qw(); use LWP::UserAgent; use HTML::Treebuilder; use HTTP::Request; 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=(); my %image = (); foreach my $image ($tree->look_down("_tag", "img")){ print $image; }
This version outputs this:
HTML::Element=HASH(0x221d7b8)HTML::Element=HASH(0x2240b30)HTML::Elemen +t=HASH(0x2240be4)HTML::Element=HASH(0x22421e8)
I am trying to build an array of these hashes that I will be using later in my script. Thanks in advance for any help!!

Edited by Chady -- linked node id.


In reply to problems constructing an array of hashes by Anonymous Monk

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.