Hi all I'm very new to perl please bare with me. I'm trying to loop values from a database and print to a text file. The table contains many columns and I'm trying to dynamically print the values from each column without having to having hard code the column names. e.g $dat_ref2->{'num'}; for each column. I need to append each value from the column and print to a text file. any help is greatly appreciated :)
my $eachQuery = "SELECT * FROM example ORDER BY num; "; my $sth2 = $epm_db->Query($eachQuery); $sth2->execute(); # get example table headings my @eachCols = @{$sth2->{NAME}}; # or NAME_lc if needed my $EachVals = ""; while ( my $dat_ref2 = $sth2->fetchrow_hashref("NAME_lc") ) { foreach my $val(@eachCols) { #print $dat_ref2->{$_}; if ($EachVals eq "") { $EachVals = $dat_ref2->{$val}; } else{ $EachVals = $EachVals."\t".$dat_ref2->{$val}; } } print MYEach "$client\t$EachVals\r"; # printing values into csv output + file }

In reply to looping hash by ibra

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.