Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: My excessive and redundant code<333

by stonecolddevin (Parson)
on Jun 23, 2005 at 02:51 UTC ( [id://469272]=note: print w/replies, xml ) Need Help??


in reply to Re: My excessive and redundant code<333
in thread My excessive and redundant code<333

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.
meh.
  • Comment on Re^2: My excessive and redundant code<333

Replies are listed 'Best First'.
Re^3: My excessive and redundant code<333
by Tanktalus (Canon) on Jun 23, 2005 at 04:07 UTC

    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. :-)

      I've been meaning to find a use for map,and here it is!!! fantastic...
      meh.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://469272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found