Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^3: My excessive and redundant code<333 by Tanktalus
in thread My excessive and redundant code<333 by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found