I'm using Class::DBI. I just don't know how to skim it down to one call of retrieve or whatever method need be called.
| [reply] |
First thing to mind is how you do the same things over and over - and I'm not sure the first thing to my mind is what's coming to your mind. ;-)
A quick perusal of Class::DBI gives me this thought. First off, it's way easier if you can keep the names the same ... so here we try:
my @comment_cols = qw/title author thread_id id/;
for (@comments) {
my %data;
@data{@comment_cols} = $_->get(@comment_cols);
push @c_data, \%data;
}
Or, if you can't keep them the same:
my %comment_cols = qw/
c_title title
author author
id thread_id
c_id id
/;
for (@comments) {
my %data;
@data{keys %comment_cols} = $_->get(values %comment_cols);
push @c_data, \%data;
}
Then push the definition (@comment_cols or %comment_cols) out into a package variable so that it only is created once, and then adding/removing/renaming columns becomes a bit easier. After all that, I also notice that anything that says "for ... push" looks like a "map" to me. So I get:
@c_data = map {
my %data;
@data{keys %comment_cols} = $_->get(values %comment_cols);
\%data;
} @comments;
Then you can take this loop or map, put it into a subroutine that takes the column list or column map as a reference, takes the input from Class::DBI as another reference, and returns the transformed data as a list of hashes. And then you'll be a long way towards making this easier to deal with, IMO. Although I have to admit, it's not really that bad now. I would just anticipate some column renamings by these steps, just to make things easier on me when I change my mind about the name of a column or something. :-) | [reply] [d/l] [select] |
I've been meaning to find a use for map,and here it is!!! fantastic...
| [reply] |