Hello Fellow Monks:

I'm writing a small web app to handle DNS requests. Everything works fine, except I'm tripping up on some data extraction for my "summary". In order to print out an ongoing summary for the user, I've created a structure similar to that found in the HTML::Template HOWTO that is an array of hash references. Primarily, this data is passed on to the main handler routine as an array reference, which is then passed onto HTML::Template so that a table can be printed out via TMPL_LOOP.

While this works fine for it's primary purpose, I'd also like to make use of this data for another sub which mails out a notice to the client. Unfortunately, I've run into difficulty while trying to deref and extract the data. I've used Data::Dumper to verify that the data is in the format I expect (an array of hashes). Here's what the data structure looks like...
$VAR1 = { 'target' => '1.1.1.1', 'id' => '1025116394', 'session' => '1025116375.79921', 'domain' => 'test.com', 'type' => 'A', 'priority' => undef, 'action' => 'add' }; $VAR2 = { 'target' => 'test.com', 'id' => '1025118001', 'session' => '1025116375.79921', 'domain' => 'www', 'type' => 'CNAME', 'priority' => undef, 'action' => 'add' };
And here are the relevant subs that create and attempt to extract the data for mailing. Can anyone tell me what I'm doing wrong here? In it's current form (I've attempted all sorts of dereferencing), I get the error "Bad index while coercing array into hash".

Thanks!
sub db_retrieve_records { my $select_query = "SELECT * from records where session=$sessi +on"; my $sth = $dbh->prepare($select_query); $sth->execute(); my %sessions; while (@records_results = $sth->fetchrow_array) { my $id = $records_results[0]; $sessions{$id}{id} = $id; $sessions{$id}{action} = $records_results[2]; $sessions{$id}{domain} = $records_results[3]; $sessions{$id}{priority} = $records_results[4]; $sessions{$id}{target} = $records_results[5]; $sessions{$id}{type} = $records_results[6]; $sessions{$id}{session} = $session; } foreach my $key (sort keys %sessions) { push(@loop,\%{$sessions{$key}}); } return \@loop; }
sub mail { local $self = shift; local $dbh = $self->param('dbh'); my $q = $self->query(); local $session = $q->param('session'); my %master_info = db_retrieve_master_info(); my @loop = db_retrieve_records(); my $head = Mail::Header->new; $head->add(From => 'somebody@localhost'); $head->add(To => $master_info{$session}{email}); $head->add(Subject => "Summary of DNS Request"); my $body .= "\n"; foreach (qw( account domain )) { $body .= "$_ $master_info{$session}{$_}\n"; } $body .= "\n"; for my $i (0..@loop) { foreach (qw( action type domain priority target )) { $body .= $loop[$i]{$_}; } $body .= "\n"; } # $body .= Data::Dumper->Dump(@loop); $body .= "Other Instructions: \n" if $master_info{$session}{ot +her}; $body .= "$master_info{$session}{other}\n"; my $mail; $mail = Mail::Internet->new(Header => $head, Body => [$body], Modify => 1, ); my $success = $mail->send('sendmail'); }

In reply to Dereferencing an array of hash references by fuzzyping

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.