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

I want to pass $header to the second foreach loop and it isn't happening. i have tried a print statement and nothing going on. Where is the $header value going? Where am i going wrong? (No comments about "trying to be a programmer", please!!!)
if ($query->param('list_name') eq 'Charges by credit card') { $ora_sthheader = $ora_dbh->prepare ( q { select distinct data_name from dwhs.store_reports where report_type = ? } ) || die "Can't prepare statement: $DBI::errstr"; $rcheader = $ora_sthheader->execute($list_name) || die "Can't execute +statement: $DBI::errstr"; print "<table border=3 cellspacing=0 cellpadding=0 width=100%>\n"; my @headers = $ora_sthheader->fetchall_arrayref(); print "<tr>\n"; foreach my $label(@$headers) { my ($header) = @$label; print "<th>$header</th>\n"; } print "</tr>\n"; $ora_sth = $ora_dbh->prepare ( q { select data_name, sum(data_value) from dwhs.store_reports where report_type = ? and timestamp >= sysdate - ? group by data_name } ) || die "Can't prepare statement: $DBI::errstr"; $rc = $ora_sth->execute($list_name,$day_list) || die "Can't execute st +atement: $DBI::errstr"; while ( ($data_name, $data_value) = $ora_sth->fetchrow_array) { $cellvalue{$data_name} = $data_value; } $row=""; foreach $header (@headers){ $row = $row . $query->td({ -bgcolor=>"#CCCCCC", -align=>"cente +r"},[$cellvalue{$header}]); print $query->Tr($row); } $ora_dbh->disconnect; print $query->end_table; print "\n"; print $query->end_html; print "\n"; }

Replies are listed 'Best First'.
(jeffa) Re: passing array value to hash not happening
by jeffa (Bishop) on Aug 16, 2001 at 01:46 UTC
    After running your code through my spaghetti strainer, the only thing i can think of is that you are forgetting to dereference $headers - and since you probably are not using strict, the typo is easily overlooked. Try this instead:
    foreach $header (@$headers) { ... }
    Hope that does it for you. :)

    Oh yeah! <plug> Take a look at DBIx::XHTML_Table </plug> ;)

    It might be as simple as:

    use strict; use DBIx::XHTML_Table; my $table = DBIx::XHTML_Table->new("insert db credentials"); $table->exec_query(" select data_name, sum(data_value) from dwhs.store_reports where report_type = ? and timestamp >= sysdate - ? group by data_name ",[$list_name,$day_list]); $table->modify_tag(table => { border => 3, cellspacing => '0', cellpadding => '0', width => '100%', }); $table->modify_tag(td => { bgcolor => '#CCCCCC', align => 'center', }); print $table->get_table();
    But this depends on what is happening in your first query that creates the table header. My module merely uses the column names as the header (i _think_ that that's what you are doing) . . . maybe i should allow a hook to replace the headers . . . but i digest. :)

    jeffa

        A flute with no holes is not a flute . . .
    a doughnut with no holes is a danish.
                                    - Basho,
                                      famous philosopher
    
(bbfu) Re: passing array value to hash not happening
by bbfu (Curate) on Aug 16, 2001 at 02:34 UTC

    my @headers = $ora_sthheader->fetchall_arrayref();

    fetchall_arrayref returns a reference to an array, which you're storing in the actual array @headers (ie, @headers contains one element, which is a reference to an array containing the data you want).

    Instead, you want:

    my $headers = $ora_sthheader->fetchall_arrayref();

    Update: Er, I mean you actually want:

    my @headers = @{$ora_sthheader->fetchall_arrayref()};

    since you later use the regular array (@headers).

    Update2: Erp, then again, you also use @$headers and $headers isn't set anywhere. You need to pick one (use $headers and dereference it later) or the other (use @headers and dereference it as you're making the function call). Your choice but you need to be consistant. Again (as jeffa noted), you should be using strict and it would've caught these errors for you.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.