I would like to take data from a table, modify it, then send it to a template (using HTML::Template) to display.

Let's say I have a text field, and I would like to censor/remove certain words (curses for example) before sending it to the template to be displayed on screen.

Example:
Contents of table row:
id = 33
author = John Doe
Text = Old McDonald had a farm

Word I want removed: McDonald, farm

I'm guessing I would somehow have to loop through the hashes keys, doing a substitue on the value that held the Text field, then assign the new value to the location in the hash... for each row that's called.

To get rows from table:

my $rows = $dbh->selectall_arrayref("SELECT id, post, view, author, last_updated, name, last_updated_by, stick,locked,? as misc
FROM someTable",{Columns=>{}},($misc));


The code to loop through the keys would probably be something like this... right?:

foreach $key (keys (%rows)) {
.....
}

But here's where I get lost... Does each key reference an array that holds the values from the table? Or how does it work exactly.
I know that a hash is basically indexed array, but what is it indexing in this case?

foreach $key (keys (%rows)) {
$item = $rows{$key} #what would this return?
}


What would $item now hold? A hash or array of elements? How would I modify them, and send the modified version to a template? I'm just trying to figure out how everything is glued together, so that I can take it apart, modify it, then glue it all back together again :-)


Basically, could someone possibly show me some sample code of pulling rows of data from a table, modifying elements of each row of the data taken from the table (for instance, if you wanted to format the date or time, or if you want to add commas to a number or something), then send the updated version of the rows to a template using HTML::Template to be displayed?


Thanks for all the help guys, I'm learning as I go, and I appreciate any help you guys might be able to offer :-)


Steny

In reply to Modifying data in a hash before sending to template by Steny

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.